博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
短信备份
阅读量:6246 次
发布时间:2019-06-22

本文共 3673 字,大约阅读时间需要 12 分钟。

短信备份: 

  • 代码实现过程 
    • 提供内容解析者把短信数据取出来
Uri uri = Uri.parse("content://sms/");            Cursor cursor = getContentResolver().query(uri, new String[]{"address", "body", "date"}, null, null, null);            Log.e(TAG, "backup: " + (cursor == null));

 

    • 使用xml序列化把数据保存到xml文件中
public class MainActivity extends AppCompatActivity {    private static final String TAG = "MainActivity";    private Button backup;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        backup = findViewById(R.id.backup);        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, 1);        } else {        }        backup.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                backup();            }        });    }    private void backup() {        try {            XmlSerializer xmlSerializer = Xml.newSerializer();            File file = new File(getFilesDir().getAbsolutePath(), "smsbackup.xml");            Log.e(TAG, "backup: " + getFilesDir().getAbsolutePath());            FileOutputStream fos = new FileOutputStream(file);            xmlSerializer.setOutput(fos, "utf-8");            xmlSerializer.startDocument("utf-8", true);            xmlSerializer.startTag(null, "smss");            Uri uri = Uri.parse("content://sms/");            Cursor cursor = getContentResolver().query(uri, new String[]{"address", "body", "date"}, null, null, null);            Log.e(TAG, "backup: " + (cursor == null));            while (cursor.moveToNext()) {                xmlSerializer.startTag(null, "sms");                xmlSerializer.startTag(null, "address");                Log.e(TAG, "backup: " + cursor.getString(0));                if (cursor.getString(0) == null || cursor.getString(0).equals("") || cursor.getString(0).equals(null)) {                    xmlSerializer.text("");                } else {                    xmlSerializer.text(cursor.getString(0));                }                xmlSerializer.endTag(null, "address");                xmlSerializer.startTag(null, "body");                Log.e(TAG, "backup: " + cursor.getString(1));                if (cursor.getString(1) == null || cursor.getString(1).equals("") || cursor.getString(1).equals(null)) {                    xmlSerializer.text("");                } else {                    xmlSerializer.text(cursor.getString(1));                }                xmlSerializer.endTag(null, "body");                xmlSerializer.startTag(null, "date");                Log.e(TAG, "backup: " + cursor.getString(2));                if (cursor.getString(2) == null || cursor.getString(2).equals("") || cursor.getString(2).equals(null)) {                    xmlSerializer.text("");                } else {                    xmlSerializer.text(cursor.getString(2));                }                xmlSerializer.endTag(null, "date");                xmlSerializer.endTag(null, "sms");                Log.e(TAG, "backup: 2222");            }            Log.e(TAG, "backup: 3333");            xmlSerializer.endTag(null, "smss");            xmlSerializer.endDocument();            Toast.makeText(this, "备份成功", Toast.LENGTH_SHORT).show();        } catch (Exception e) {            Log.e(TAG, "backup: " + e.getLocalizedMessage().toString());            Toast.makeText(this, "备份失败", Toast.LENGTH_SHORT).show();            e.printStackTrace();        }    }}

 

转载于:https://www.cnblogs.com/nangongyibin/p/10233850.html

你可能感兴趣的文章
日志分析工具ELK(三)
查看>>
PAT (Advanced Level) 1049. Counting Ones (30)
查看>>
HDU 5763 Another Meaning
查看>>
session详解
查看>>
scroll滚动条
查看>>
mysql链接超时错误
查看>>
Win10 安装 Linux子系统 Ubuntu18.04 / Kali Linux 的体验
查看>>
才发现用git Gui也能上传项目到gitHub,下面分享一下经验
查看>>
P1462 通往奥格瑞玛的道路
查看>>
关于ajax
查看>>
一份关于jvm内存调优及原理的学习笔记
查看>>
怎么查看80端口占用情况- 如何查看端口占用情况?
查看>>
搭建测试框架
查看>>
position:absolute在IE8浏览器下无法显示正确位置
查看>>
过滤器与拦截器区别
查看>>
C# 使用 Windows API 发送文件到打印机
查看>>
NOIP2013 D1T3 货车运输 倍增LCA OR 并查集按秩合并
查看>>
80端口被NT kernel & System 占用pid 4
查看>>
mat工具MemoryAnalyzer进行分析java内存溢出hprof文件
查看>>
完整性约束
查看>>