自制Android中的短息备份器源码

短信的实体bean

package com.examp.backsms.domain;

public class SmsInfo {
	private String address;
	private String date;
	private String type;
	private String body;

	public SmsInfo() {
	}

	public SmsInfo(String address, String date, String type, String body) {
		this.address = address;
		this.date = date;
		this.type = type;
		this.body = body;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	@Override
	public String toString() {
		return "SmsInfo [address=" + address + ", date=" + date + ", type="
				+ type + ", body=" + body + "]";
	}

}

核心代码

package com.examp.backsms;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.examp.backsms.domain.SmsInfo;
import com.examp.backsms.util.SmsUtils;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void backSms(View view) {

        // 创建被叫主机名
        Uri uri = Uri.parse("content://sms/");
        // 获取内容解析器
        ContentResolver resolver = getContentResolver();
        // 执行查询,得到结果集
        Cursor cursor = resolver.query(uri, new String[] { "address", "date",
                "type", "body" }, null, null, null);
         //创建短信的集合
         List<SmsInfo> infos = new ArrayList<SmsInfo>();
         //逐一添加到集合
         while (cursor.moveToNext()) {
            SmsInfo info = new SmsInfo();
            info.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            info.setBody(cursor.getString(cursor.getColumnIndex("body")));
            info.setDate(cursor.getString(cursor.getColumnIndex("date")));
            info.setType(cursor.getString(cursor.getColumnIndex("type")));
            infos.add(info);
        }
        //通过工具类调用备份的业务
        //将集合|本活动的上下文传入
         SmsUtils.backupSms(infos, this);
        cursor.close();
    }

}



工具类

package com.examp.backsms.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import android.widget.Toast;

import com.examp.backsms.domain.SmsInfo;

public class SmsUtils {
	public static void backupSms(List<SmsInfo> smsInfos, Context context) {
		try {
			// 创建XML序列化器
			XmlSerializer serializer = Xml.newSerializer();
			File file = new File(Environment.getExternalStorageDirectory(),
					"backupSms.xml");
			FileOutputStream os = new FileOutputStream(file);
			// 初始化xml序列化器,指定xml数据写入到那个文件,并且制定文件的编码方式
			serializer.setOutput(os, "utf-8");
			// 设置<?xml version="1.0" encoding="utf-8"?>,及是否是独立的xml文件
			serializer.startDocument("utf-8", true);
			// 相当于<smss>
			serializer.startTag(null, "smss");
			for (SmsInfo info : smsInfos) {
				serializer.startTag(null, "sms");

				serializer.startTag(null, "body");
				serializer.text(info.getBody());
				serializer.endTag(null, "body");

				serializer.startTag(null, "type");
				serializer.text(info.getType() + "");
				serializer.endTag(null, "type");

				serializer.startTag(null, "address");
				serializer.text(info.getAddress());
				serializer.endTag(null, "address");

				serializer.startTag(null, "date");
				serializer.text(info.getDate() + "");
				serializer.endTag(null, "date");

				serializer.endTag(null, "sms");
			}
			serializer.endTag(null, "smss");
			serializer.endDocument();
			os.close();
			Toast.makeText(
					context,
					"备份短信成功,备份储存路径为:"
							+ Environment.getExternalStorageDirectory()
							+ "backSms.xml", Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			Toast.makeText(context, "备份短信息失败!!", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}
}

视图代码,由于只是一个案例,只有一个按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="backSms"
        android:text="@string/bt_queryAll" />

</RelativeLayout>

权限清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.examp.backsms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <!-- 获取 读写短信的权限 -->
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <!-- 获取写入SD卡的权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.examp.backsms.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

资源文件string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">0019短信备份</string>
    <string name="action_settings">Settings</string>
    <string name="bt_queryAll">备份短信</string>

</resources>

注:本代码只是事例代码,不能够承受过大的数据压力,所以实际开发的时候是边读数据边备份.




自制Android中的短息备份器源码,布布扣,bubuko.com

自制Android中的短息备份器源码

上一篇:60款开源云应用【Part 1】(60 Open Source Apps You Can Use in the Cloud)


下一篇:android多分辨率适配