短信备份助手

来源:互联网 发布:希赛网络软考学院 编辑:程序博客网 时间:2024/04/29 05:54
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.arvin.smsbackup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_SMS"/>         增加权限
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <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.arvin.smsbackup.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>
布局
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >


    <Button
        android:onClick="smsBackup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="短信备份" />

    <Button
        android:onClick="smsRestore"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="短信还原" />

</LinearLayout>
主界面


package com.arvin.smsbackup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    /**
     * 短信的备份
     *
     * @param view
     */
    public void smsBackup(View view) {
        try {
            // 利用内容提供者获取短信私有的数据
            ContentResolver resovler = getContentResolver();
            Uri uri = Uri.parse("content://sms/");
            Cursor cursor = resovler.query(uri, new String[] { "address",
                    "date", "type", "body" }, null, null, null);
            XmlSerializer serializer = Xml.newSerializer();
            File file = new File(Environment.getExternalStorageDirectory(),
                    "backup.xml");
            FileOutputStream os = new FileOutputStream(file);
            serializer.setOutput(os, "utf-8");
           
            //serializer.startTag( namespace, name); 命名空间   标签名
            // 写xml文件的头  
            serializer.startDocument("utf-8", true);  
            serializer.startTag(null, "root");               
            while (cursor.moveToNext()) {
                serializer.startTag(null, "sms");
                serializer.startTag(null, "address");
                String address = cursor.getString(0);
                serializer.text(address);
                serializer.endTag(null, "address");
                serializer.startTag(null, "date");
                String date = cursor.getString(1);
                serializer.text(date);
                serializer.endTag(null, "date");

                serializer.startTag(null, "type");
                String type = cursor.getString(2);
                serializer.text(type);
                serializer.endTag(null, "type");

                serializer.startTag(null, "body");
                String body = cursor.getString(3);
                serializer.text(body);
                serializer.endTag(null, "body");

                serializer.endTag(null, "sms");
            }
            cursor.close();
            serializer.endTag(null, "root");
            serializer.endDocument();
            os.close();
            Toast.makeText(this, "备份成功", 0).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "备份失败", 0).show();
        }
    }

    /**
     * 短信的还原
     *
     * @param view
     */
    public void smsRestore(View view) {

        // file.lastModified(); 获取文件上一次备份的时间。
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("提醒");
        builder.setMessage("是否清除旧的短信?");
        builder.setPositiveButton("确定清除", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Uri uri = Uri.parse("content://sms/");
                getContentResolver().delete(uri, null, null);
                restore();
            }
        });
        builder.setNegativeButton("不清除数据", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                restore();
            }
        });
        builder.show();
    }

    /**
     * 还原所有备份的短信。
     */
    protected void restore() {
        try {
            File file = new File(Environment.getExternalStorageDirectory(),
                    "backup.xml");
            FileInputStream fis = new FileInputStream(file);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(fis, "utf-8");
            int type = parser.getEventType();
            String address = null;
            String date= null;
            String smstype= null;
            String body= null;
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                case XmlPullParser.START_TAG:
                    if("type".equals(parser.getName())){
                        smstype = parser.nextText();
                    }else if("body".equals(parser.getName())){
                        body = parser.nextText();
                    }else if("date".equals(parser.getName())){
                        date = parser.nextText();
                    }else if("address".equals(parser.getName())){
                        address = parser.nextText();
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if("sms".equals(parser.getName())){
                        //一条短信的数据结束,把短信的数据加入到系统的短信应用数据库
                        ContentResolver resolver = getContentResolver();
                        ContentValues values = new ContentValues();
                        values.put("address", address);
                        values.put("body", body);
                        values.put("type", smstype);
                        values.put("date", date);
                        resolver.insert(Uri.parse("content://sms/"), values);
                    }
                    break;
                }
                type = parser.next();
            }
            Toast.makeText(this, "还原短信成功", 0).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "还原短信失败", 0).show();
        }
    }

}
























0 0
原创粉丝点击