package com.example.sms; import java.io.File; import java.io.FileOutputStream; import org.xmlpull.v1.XmlSerializer; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.util.Xml; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //查 public void query(View v) {//查询手机短信 // 1. 获取内容解析器 ContentResolver resolver = getContentResolver(); // 2. 指定uri Uri uri = Uri.parse("content://sms/"); // 3. 查询 Cursor cursor = resolver.query(uri, new String[] { "address", "body","read", "date", "type" }, null, null, null); while (cursor.moveToNext()) { String address = cursor.getString(0); String body = cursor.getString(1); String read = cursor.getString(2); String date = cursor.getString(3); String type = cursor.getString(4); SmsBean smsBean = new SmsBean(address, date, body, type, read); System.out.println(smsBean.toString()); } } /** * 1. 读取系统短信 2. 用xml文件备份系统短信 */ //备份 public void backup(View v) { // 1. 获取内容解析器 ContentResolver resolver = getContentResolver(); // 2. 指定uri Uri uri = Uri.parse("content://sms/"); // 3. 查询 Cursor cursor = resolver.query(uri, new String[] { "address", "body","read", "date", "type" }, null, null, null); try { // 初始化序列化器 XmlSerializer serializer = Xml.newSerializer(); // 设置参数 FileOutputStream os = new FileOutputStream(new File("/mnt/sdcard/sms.xml")); serializer.setOutput(os, "utf-8"); // 写数据 serializer.startDocument("utf-8", true); // 跟标签 serializer.startTag(null, "smss"); while (cursor.moveToNext()) { String address = cursor.getString(0); String body = cursor.getString(1); String read = cursor.getString(2); String date = cursor.getString(3); String type = cursor.getString(4); serializer.startTag(null, "sms"); // 手机号码 serializer.startTag(null, "address"); serializer.text(address); serializer.endTag(null, "address"); // 短信内容 serializer.startTag(null, "body"); serializer.text(body); serializer.endTag(null, "body"); // 短信阅读状态 serializer.startTag(null, "read"); serializer.text(read); serializer.endTag(null, "read"); // 短息收发时间 serializer.startTag(null, "date"); serializer.text(date); serializer.endTag(null, "date"); // 短信的收发状态 serializer.startTag(null, "type"); serializer.text(type); serializer.endTag(null, "type"); serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); os.close(); Toast.makeText(this, "短信存储成功!/mnt/sdcard/sms.xml", 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "失败啦,短信神器不流弊啊!", 0).show(); } } //插 public void insert(View v) { // 1. 获取内容解析器 ContentResolver resolver = getContentResolver(); // 2. 指定uri Uri uri = Uri.parse("content://sms/"); ContentValues values = new ContentValues(); values.put("address", "13112345678"); values.put("read", 0); values.put("type", 1); values.put("body", "我是在代码中插入的短信"); values.put("date", System.currentTimeMillis()); resolver.insert(uri, values); } } //删 public void delete(View v) { int id = resolver.delete(uri, null, null); } //改 public void updata(View v) { System.out.println(11); ContentValues values = new ContentValues(); values.put("address", "10000"); System.out.println(22); resolver.update(uri, values, "address = ? ", new String[] { "10086" }); } //备份还原短信 解析XML文件 public void restore(View v) { XmlPullParser parser = Xml.newPullParser(); FileInputStream is; try { list = new ArrayList<MessageBean>(); MessageBean bean = null; String address = null; String read = null; String date = null; String type1 = null; String body = null;; is = new FileInputStream(new File("/mnt/sdcard/message.xml")); parser.setInput(is, "utf-8"); int type = parser.getEventType(); while (type != XmlPullParser.END_DOCUMENT) { String tag = parser.getName(); switch (type) { case XmlPullParser.START_TAG: if(tag.equals("sms")){ bean = new MessageBean(); } if (tag.equals("address")) { address = parser.nextText(); bean.setAddress(address); } else if (tag.equals("read")) { read = parser.nextText(); bean.setRead(read); } else if (tag.equals("date")) { date = parser.nextText(); bean.setDate(date); } else if (tag.equals("type")) { type1 = parser.nextText(); bean.setType(type1+""); } else if (tag.equals("body")) { body = parser.nextText(); bean.setBody(body); list.add(bean); System.out.println(list+"-------------"); } break; default: break; } type = parser.next(); } is.close(); for (int i = 0; i < list.size(); i++) { ContentValues values = new ContentValues(); values.put("address",list.get(i).getAddress()); values.put("date", list.get(i).getDate()); values.put("body", list.get(i).getBody()); values.put("read", list.get(i).getRead()); values.put("type", list.get(i).getType()); resolver.insert(uri, values); } } catch (Exception e) { e.printStackTrace(); } } }
package com.example.sms; public class SmsBean { private String address; private String date; private String body; private String type; private String read; public SmsBean(String address, String date, String body, String type,String read) { super(); this.address = address; this.date = date; this.body = body; this.type = type; this.read = read; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getRead() { return read; } public void setRead(String read) { this.read = read; } @Override public String toString() { return "SmsBean [address=" + address + ", date=" + date + ", body="+ body + ", type=" + type + ", read=" + read + "]"; } }
清单文件中加相应的权限: <uses-permission android:name="android.permission.READ_SMS"/> <uses-permission android:name="android.permission.WRITE_SMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
时间: 2024-10-06 21:58:52