Android短信备份及插入笔记
实现备份短信到xml文件和像短信中插入一条数据
一、实现短信将备份到xml文件中
在布局文件中定义一个按钮,定义点击事件为copyClick
MainActivity.java:
1 package com.lgqrlchinese.heima76android_copysms; 2 3 import android.Manifest; 4 import android.content.pm.PackageManager; 5 import android.database.Cursor; 6 import android.net.Uri; 7 import android.os.Environment; 8 import android.support.v4.app.ActivityCompat; 9 import android.support.v4.content.ContextCompat; 10 import android.support.v7.app.AppCompatActivity; 11 import android.os.Bundle; 12 import android.util.Xml; 13 import android.view.View; 14 import android.widget.Toast; 15 16 import org.xmlpull.v1.XmlSerializer; 17 18 import java.io.File; 19 import java.io.FileNotFoundException; 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 23 public class MainActivity extends AppCompatActivity { 24 //动态获取权限 25 static final String[] PERMISSION = new String[]{ 26 Manifest.permission.READ_SMS, 27 Manifest.permission.WRITE_EXTERNAL_STORAGE, 28 }; 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.activity_main); 34 35 //动态获取权限 36 MyPermission(); 37 } 38 39 //点击按钮查询短信数据库内容,然后备份 40 public void copyClick(View view) { 41 42 try { 43 //获取xml序列化实例 44 XmlSerializer serializer = Xml.newSerializer(); 45 //设置序列化参数 46 File file = new File(Environment.getExternalStorageDirectory().getPath(), "smsBackUp.xml"); 47 FileOutputStream fos = new FileOutputStream(file); 48 serializer.setOutput(fos, "utf-8"); 49 //开始写xml文档的开头 50 serializer.startDocument("utf-8", true); 51 //开始写根节点 52 serializer.startTag(null, "smss"); 53 54 //短信数据库内容也通过内容提供者暴露出来,所以我们只需要通过内容解析者去操作数据库 55 Uri uri = Uri.parse("content://sms/"); 56 Cursor cursor = getContentResolver().query(uri, new String[]{"address", "date", "body"}, null, null, null); 57 while (cursor.moveToNext()) { 58 String address = cursor.getString(0); 59 String date = cursor.getString(1); 60 String body = cursor.getString(2); 61 62 //写sms节点 63 serializer.startTag(null, "sms"); 64 65 //写address节点 66 serializer.startTag(null, "address"); 67 serializer.text(address); 68 serializer.endTag(null, "address"); 69 70 //写body节点 71 serializer.startTag(null, "body"); 72 serializer.text(body); 73 serializer.endTag(null, "body"); 74 75 //写date节点 76 serializer.startTag(null, "date"); 77 serializer.text(date); 78 serializer.endTag(null, "date"); 79 80 serializer.endTag(null, "sms"); 81 82 } 83 84 serializer.endTag(null, "smss"); 85 serializer.endDocument(); 86 87 88 } catch (FileNotFoundException e) { 89 e.printStackTrace(); 90 System.out.println("异常1"); 91 } catch (IOException e) { 92 e.printStackTrace(); 93 System.out.println("异常2"); 94 } 95 96 97 } 98 99 //动态获取权限 100 public void MyPermission() { 101 if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 102 //Android 6.0申请权限 103 ActivityCompat.requestPermissions(this, PERMISSION, 1); 104 } else { 105 Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show(); 106 } 107 } 108 109 }
当然权限是必要的
1 <uses-permission android:name="android.permission.READ_SMS"/> 2 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这样就在SD卡根目录备份了xml文件
在真机6.0测试中遇到存储address崩溃问题,调试好久。先打印addredss,没有问题;存储死数据没有问题,最后发现在输出打印室有个address为null。查看手机,是因为有个短信草稿,我想了两个都能想到的的解决办法:
1.删除或发送手机里的草稿
2.在代码中进行判断
1 if (address != null) { 2 3 serializer.startTag(null, "address"); 4 serializer.text(address); 5 serializer.endTag(null, "address"); 6 } else { 7 serializer.text("这是草稿"); 8 }
二、向短信的数据库中插入数据(5.0及之后就不允许外部应用修改,直允许读取,不做过多记录)
1 import android.Manifest; 2 import android.content.ContentValues; 3 import android.content.pm.PackageManager; 4 import android.net.Uri; 5 import android.support.v4.app.ActivityCompat; 6 import android.support.v4.content.ContextCompat; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.Toast; 11 12 public class MainActivity extends AppCompatActivity { 13 //动态获取权限 14 static final String[] PERMISSION = new String[]{ 15 Manifest.permission.READ_SMS, 16 }; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 MyPermission(); 23 } 24 25 //点击按钮在短信数据库插入数据 26 public void insertClick(View view) { 27 Uri uri = Uri.parse("ocntent://sms"); 28 ContentValues values = new ContentValues(); 29 values.put("address", "17865318803"); 30 values.put("body", "我是你的影子"); 31 values.put("date", System.currentTimeMillis()); 32 getContentResolver().insert(uri, values); 33 } 34 35 //动态获取权限 36 public void MyPermission() { 37 if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 38 //Android 6.0申请权限 39 ActivityCompat.requestPermissions(this, PERMISSION, 1); 40 } else { 41 Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show(); 42 } 43 } 44 }
昔日我曾苍老,如今风华正茂(ง •̀_•́)ง