Android短信备份及数据插入实现代码解析
程序员文章站
2022-09-05 12:25:04
实现备份短信到xml文件和像短信中插入一条数据一、实现短信将备份到xml文件中在布局文件中定义一个按钮,定义点击事件为copyclickmainactivity.java:package com.lg...
实现备份短信到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(); } } }
当然权限是必要的
<uses-permission android:name="android.permission.read_sms"/>
<uses-permission android:name="android.permission.write_external_storage"/>
这样就在sd卡根目录备份了xml文件
在真机6.0测试中遇到存储address崩溃问题,调试好久。先打印addredss,没有问题;存储死数据没有问题,最后发现在输出打印室有个address为null。查看手机,是因为有个短信草稿,我想了两个都能想到的的解决办法:
1.删除或发送手机里的草稿
2.在代码中进行判断
if (address != null) { serializer.starttag(null, "address"); serializer.text(address); serializer.endtag(null, "address"); } else { serializer.text("这是草稿"); }
二、向短信的数据库中插入数据(5.0及之后就不允许外部应用修改,直允许读取,不做过多记录)
import android.manifest; import android.content.contentvalues; import android.content.pm.packagemanager; import android.net.uri; import android.support.v4.app.activitycompat; import android.support.v4.content.contextcompat; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.toast; public class mainactivity extends appcompatactivity { //动态获取权限 static final string[] permission = new string[]{ manifest.permission.read_sms, }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mypermission(); } //点击按钮在短信数据库插入数据 public void insertclick(view view) { uri uri = uri.parse("ocntent://sms"); contentvalues values = new contentvalues(); values.put("address", "17865318803"); values.put("body", "我是你的影子"); values.put("date", system.currenttimemillis()); getcontentresolver().insert(uri, values); } //动态获取权限 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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。