[android] 短信的备份

移动开发 作者: 2024-08-19 20:00:01
使用ddms给模拟器发送几条短信,找到短信数据库导出 /data/data/com.android.providers.telephony/databases/mmssms.db 数据库字段:addr
package com.tsh.backsmstrue;

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

 com.tsh.backsmstrue.domain.SmsInfo;
 com.tsh.backsmstrue.utils.SmsUtil;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**
     * 备份短信
     * @param v
     */
     backupSms(View v){
        Uri uri=Uri.parse("content://sms/");
        ContentResolver resolver=getContentResolver();
        String[] projection=new String[]{"address","date","type","body"};
        Cursor cursor=resolver.query(uri,null,1)">null);
        List<SmsInfo> smss=new ArrayList<SmsInfo>();
        while(cursor.moveToNext()){
            String body=cursor.getString(cursor.getColumnIndex("body"));
            System.out.println(body);
            System.out.println("===========");
            SmsInfo smsinfo=new SmsInfo();
            smsinfo.setAddress("110");
            smsinfo.setMessage(body);
            smss.add(smsinfo);
        }
        SmsUtil.backSms2(smss,this);
        cursor.close();
    }
}
 com.tsh.backsmstrue.utils;

 java.io.File;
 java.io.FileOutputStream;
 org.xmlpull.v1.XmlSerializer;

 com.tsh.backsmstrue.domain.SmsInfo;

 android.content.Context;
 android.os.Environment;
 android.util.Xml;
 android.view.View;
 android.widget.Toast;


class SmsUtil {
    
     * 备份短信第二种
     * static void backSms2(List<SmsInfo> smsInfos,Context context){
        XmlSerializer serializer=Xml.newSerializer();
        try {
            File file=new File(Environment.getExternalStorageDirectory(),"backup2.xml");
            FileOutputStream os= FileOutputStream(file);
            serializer.setOutput(os,"utf-8");
            serializer.startDocument("utf-8",1)">true);
            serializer.startTag();
            for(SmsInfo sms:smsInfos){
                serializer.startTag();
                serializer.attribute(,sms.getAddress());
                serializer.text(sms.getMessage());
                serializer.endTag();
            }
            serializer.endTag();
            serializer.endDocument();
            os.close();
            Toast.makeText(context,"保存成功",0).show();
        } catch (Exception e) {
            Toast.makeText(context,"保存失败",1)">).show();
        }
    }
}

总结

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65118.html
[android] 短信的备份