安卓笔记之查询、备份、插入系统短信数据库
package com.example.day0328_getsms; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlSerializer; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.util.Log; import android.util.Xml; import android.view.View; public class MainActivity extends Activity { List<Message> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList<Message>(); } public void click(View v){ //获取内容解析器 ContentResolver cr = getContentResolver(); //content://sms可以理解为短信主机名 Cursor cursor =cr.query(Uri.parse("content://sms"), new String[]{"address","date","body","type"}, null, null, null); //查询 while(cursor.moveToNext()){ String address = cursor.getString(cursor.getColumnIndex("address")); long date = cursor.getLong(cursor.getColumnIndex("date")); String body = cursor.getString(cursor.getColumnIndex("body")); String type = cursor.getString(cursor.getColumnIndex("type")); //测试一下查询结果 Log.i("aa", address+","+date+","+body+","+type); //用一个Javbean把数据封装到Message里面 Message message = new Message(address, date, body, type); //添加到集合List里面 list.add(message); } } public void click2(View v){ //获取Xml寄存器 XmlSerializer xs = Xml.newSerializer(); //创建一个文件 File file = new File(getCacheDir(), "sms.xml"); //拿到它的文件输出流 FileOutputStream fos=null; try { fos = new FileOutputStream(file); //告诉寄存器对象写到哪里并指定其编码 xs.setOutput(fos, "UTF-8"); //开始节点 xs.startDocument("UTF-8", true); //开始标签 xs.startTag(null, "message"); //遍历message for(Message sms :list){ xs.startTag(null, "sms"); xs.startTag(null, "address"); xs.text(sms.getAddress()); xs.endTag(null, "address"); xs.startTag(null, "date"); xs.text(sms.getDate()+""); xs.endTag(null, "date"); xs.startTag(null, "body"); xs.text(sms.getBody()); xs.endTag(null, "body"); xs.startTag(null, "type"); xs.text(sms.getType()); xs.endTag(null, "type"); xs.endTag(null, "sms"); } //结束标签 xs.endTag(null, "message"); //结束节点 xs.endDocument(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void click3(View v){ //拿到内容解析器 ContentResolver cr = getContentResolver(); //拿到Contentvalus对象并插入内容 ContentValues vaulus = new ContentValues(); vaulus.put("address", "10086"); vaulus.put("date", System.currentTimeMillis()); vaulus.put("body", "你好,你尾号为xxx的建设银行卡收到了来自xxx转发的1,000,000RMB,请注意查收"); cr.insert(Uri.parse("content://sms"), vaulus); } } //权限 android.permission.READ_SMS android.permission.WRITE_SMS //布局 <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取系统短信" android:onClick="click"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="备份系统短信" android:onClick="click2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="插入短信" android:onClick="click3" /> </LinearLayout>