欢迎光临八叔引擎之家,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!

八叔引擎之家

[android] 短信的备份
移动开发 2021-02-16
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.viiis.cn/news/show_23668.html
[android] 短信的备份

本站采用系统自动发货方式,付款后即出现下载入口,如有疑问请咨询在线客服!

售后时间:早10点 - 晚11:30点

咨询售后客服

服务热线 19970861797
服务热线 19970861797服务热线 19970861797
手机二维码
返回顶部
返回顶部返回顶部