Android开发中如何实现短信备份功能-创新互联
这期内容当中小编将会给大家带来有关Android开发中如何实现短信备份功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
莲池ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!实现备份短信到xml文件和像短信中插入一条数据
一、实现短信将备份到xml文件中
在布局文件中定义一个按钮,定义点击事件为copyClick
MainActivity.java:
package com.lgqrlchinese.heima76android_copysms; import android.Manifest; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Xml; import android.view.View; import android.widget.Toast; import org.xmlpull.v1.XmlSerializer; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { //动态获取权限 static final String[] PERMISSION = new String[]{ Manifest.permission.READ_SMS, Manifest.permission.WRITE_EXTERNAL_STORAGE, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //动态获取权限 MyPermission(); } //点击按钮查询短信数据库内容,然后备份 public void copyClick(View view) { try { //获取xml序列化实例 XmlSerializer serializer = Xml.newSerializer(); //设置序列化参数 File file = new File(Environment.getExternalStorageDirectory().getPath(), "smsBackUp.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "utf-8"); //开始写xml文档的开头 serializer.startDocument("utf-8", true); //开始写根节点 serializer.startTag(null, "smss"); //短信数据库内容也通过内容提供者暴露出来,所以我们只需要通过内容解析者去操作数据库 Uri uri = Uri.parse("content://sms/"); Cursor cursor = getContentResolver().query(uri, new String[]{"address", "date", "body"}, null, null, null); while (cursor.moveToNext()) { String address = cursor.getString(0); String date = cursor.getString(1); String body = cursor.getString(2); //写sms节点 serializer.startTag(null, "sms"); //写address节点 serializer.startTag(null, "address"); serializer.text(address); serializer.endTag(null, "address"); //写body节点 serializer.startTag(null, "body"); serializer.text(body); serializer.endTag(null, "body"); //写date节点 serializer.startTag(null, "date"); serializer.text(date); serializer.endTag(null, "date"); serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("异常1"); } catch (IOException e) { e.printStackTrace(); System.out.println("异常2"); } } //动态获取权限 public void MyPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { //Android 6.0申请权限 ActivityCompat.requestPermissions(this, PERMISSION, 1); } else { Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show(); } } }
文章标题:Android开发中如何实现短信备份功能-创新互联
文章转载:http://azwzsj.com/article/cchhse.html