接着上文《Android 内容提供者的实现》,继续实战
打开File Exploer,找到mmssms.db数据库,导出
打开mmssms.db
新建项目,布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取所有的短信" /> </RelativeLayout>
代码如下:
package com.wuyudong.readsms; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view) { // content://sms/ Uri uri = Uri.parse("content://sms/"); ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(uri, new String[] { "address", "date", "type", "body" }, null, null, null); while (cursor.moveToNext()) { String address = cursor.getString(0); String date = cursor.getString(1); String type = cursor.getString(2); String body = cursor.getString(3); System.out.println("address:" + address); System.out.println("date:" + date); System.out.println("type:" + type); System.out.println("body:" + body); System.out.println("--------------------"); } cursor.close(); } }
运行之,提示错误:权限问题。于是添加权限android.permission.READ_SMS和android.permission.WRITE_SMS
再次运行,搞定,成功读取系统短信
接下来继续修改程序,使之完成短信的备份功能
新建文件smsInfo.java
package com.wuyudong.domain; public class smsInfo { private String body; private String date; private String type; private String address; public smsInfo(String body, String date, String type, String address) { super(); this.body = body; this.date = date; this.type = type; this.address = address; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
新建SmsUtils.java
package com.wuyudong.readsms; import java.io.File; import java.io.FileOutputStream; import java.util.List; import org.xmlpull.v1.XmlSerializer; import com.wuyudong.domain.smsInfo; import android.content.Context; import android.os.Environment; import android.util.Xml; import android.widget.Toast; public class SmsUtils { /** * * @param smsInfos 短信的集合 * @param context 上下文 */ public static void backUpSms(List<smsInfo> smsInfos, Context context){ File file = new File(Environment.getExternalStorageDirectory(), "backup1.xml"); try { FileOutputStream fos = new FileOutputStream(file); // 获取xml序列化器 XmlSerializer xs = Xml.newSerializer(); xs.setOutput(fos, "utf-8"); //生成xml头 xs.startDocument("utf-8", true); //添加xml根节点 xs.startTag(null, "message"); for (smsInfo info : smsInfos) { xs.startTag(null, "sms"); xs.startTag(null, "body"); xs.text(info.getBody()); xs.endTag(null, "body"); xs.startTag(null, "date"); xs.text(info.getDate()); xs.endTag(null, "date"); xs.startTag(null, "address"); xs.text(info.getAddress()); xs.endTag(null, "address"); xs.startTag(null, "type"); xs.text(info.getType()); xs.endTag(null, "type"); xs.endTag(null, "sms"); } xs.endTag(null, "message"); //生成xml头 xs.endDocument(); fos.close(); Toast.makeText(context, "备份成功", 0).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(context, "备份失败", 0).show(); } } }
添加权限:android.permission.WRITE_EXTERNAL_STORAGE
MainActivity.java中的代码如下:
package com.wuyudong.readsms; import java.util.ArrayList; import java.util.List; import com.wuyudong.domain.smsInfo; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view) { // content://sms/ Uri uri = Uri.parse("content://sms/"); ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(uri, new String[] { "body", "date", "type", "address" }, null, null, null); List<smsInfo> smsinfos = new ArrayList<smsInfo>(); while (cursor.moveToNext()) { String body = cursor.getString(0); String date = cursor.getString(1); String type = cursor.getString(2); String address = cursor.getString(3); smsInfo smsinfo = new smsInfo(body, date, type, address); smsinfos.add(smsinfo); } cursor.close(); SmsUtils.backUpSms(smsinfos, this); } }
运行项目后,生成backup1.xml文件,打开后
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message> <sms> <body>ggg</body> <date>1466043889225</date> <address>1223</address> <type>1</type> </sms> <sms> <body>dsfgfdg</body> <date>1466043867803</date> <address>34543</address> <type>1</type> </sms> …………………… …………………… …………………… </message>