1.短信备份

package com.su.smsbackuprestore.operatesms;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.xmlpull.v1.XmlSerializer;import android.content.ContentResolver;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteException;import android.net.Uri;import android.os.Environment;import android.util.Log;import android.util.Xml;import android.widget.Toast;public class ExportSmsXml {Context context;public static final String SMS_URI_ALL = "content://sms/";private FileOutputStream outStream = null;private XmlSerializer serializer;public ExportSmsXml(Context context) {this.context = context;}public void xmlStart() {String path = Environment.getExternalStorageDirectory().getPath() + "/SMSBackup";File file = new File(path);if (!file.exists()) {file.mkdirs();}File file2 = new File(path, "message.xml");try {outStream = new FileOutputStream(file2);serializer = Xml.newSerializer();serializer.setOutput(outStream, "UTF-8");serializer.startDocument("UTF-8", true);serializer.startTag(null, "sms");} catch (Exception e) {e.printStackTrace();}}public boolean createXml() throws Exception {this.xmlStart();Cursor cursor = null;try {ContentResolver conResolver = context.getContentResolver();String[] projection = new String[] { SmsField.ADDRESS, SmsField.PERSON, SmsField.DATE, SmsField.PROTOCOL, SmsField.READ, SmsField.STATUS,SmsField.TYPE, SmsField.REPLY_PATH_PRESENT,SmsField.BODY,SmsField.LOCKED,SmsField.ERROR_CODE, SmsField.SEEN }; // type=1是收件箱,==2是发件箱;read=0表示未读,read=1表示读过,seen=0表示未读,seen=1表示读过Uri uri = Uri.parse(SMS_URI_ALL);cursor = conResolver.query(uri, projection, null, null, "_id asc");if (cursor.moveToFirst()) {// 查看数据库sms表得知 subject和service_center始终是null所以这里就不获取它们的数据了。String address;String person;String date;String protocol;String read;String status;String type;String reply_path_present;String body;String locked;String error_code;String seen;do {// 如果address == null,xml文件中是不会生成该属性的,为了保证解析时,属性能够根据索引一一对应,必须要保证所有的item标记的属性数量和顺序是一致的address = cursor.getString(cursor.getColumnIndex(SmsField.ADDRESS));if (address == null) {address = "";}person = cursor.getString(cursor.getColumnIndex(SmsField.PERSON));if (person == null) {person = "";}date = cursor.getString(cursor.getColumnIndex(SmsField.DATE));if (date == null) {date = "";}protocol = cursor.getString(cursor.getColumnIndex(SmsField.PROTOCOL));if (protocol == null) {// 为了便于xml解析protocol = "";}read = cursor.getString(cursor.getColumnIndex(SmsField.READ));if (read == null) {read = "";}status = cursor.getString(cursor.getColumnIndex(SmsField.STATUS));if (status == null) {status = "";}type = cursor.getString(cursor.getColumnIndex(SmsField.TYPE));if (type == null) {type = "";}reply_path_present = cursor.getString(cursor.getColumnIndex(SmsField.REPLY_PATH_PRESENT));if (reply_path_present == null) {// 为了便于XML解析reply_path_present = "";}body = cursor.getString(cursor.getColumnIndex(SmsField.BODY));if (body == null) {body = "";}locked = cursor.getString(cursor.getColumnIndex(SmsField.LOCKED));if (locked == null) {locked = "";}error_code = cursor.getString(cursor.getColumnIndex(SmsField.ERROR_CODE));if (error_code == null) {error_code = "";}seen = cursor.getString(cursor.getColumnIndex(SmsField.SEEN));if (seen == null) {seen = "";}// 生成xml子标记// 开始标记serializer.startTag(null, "item");// 加入属性serializer.attribute(null, SmsField.ADDRESS, address);serializer.attribute(null, SmsField.PERSON, person);serializer.attribute(null, SmsField.DATE, date);serializer.attribute(null, SmsField.PROTOCOL, protocol);serializer.attribute(null, SmsField.READ, read);serializer.attribute(null, SmsField.STATUS, status);serializer.attribute(null, SmsField.TYPE, type);serializer.attribute(null, SmsField.REPLY_PATH_PRESENT, reply_path_present);serializer.attribute(null, SmsField.BODY, body);serializer.attribute(null, SmsField.LOCKED, locked);serializer.attribute(null, SmsField.ERROR_CODE, error_code);serializer.attribute(null, SmsField.SEEN, seen);// 结束标记serializer.endTag(null, "item");} while (cursor.moveToNext());} else {return false;}} catch (SQLiteException ex) {ex.printStackTrace();Log.d("SQLiteException:", ex.getMessage());}finally {if(cursor != null) {cursor.close();//手动关闭cursor,及时回收}}serializer.endTag(null, "sms");serializer.endDocument();outStream.flush();outStream.close();return true;}}

2.短信恢复

package com.su.smsbackuprestore.operatesms;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import android.content.ContentResolver;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.net.Uri;import android.os.Environment;import android.os.Looper;import android.util.Log;import android.util.Xml;import android.widget.Toast;public class ImportSms {private Context context;private List<SmsItem> smsItems;private ContentResolver conResolver;public ImportSms(Context context) {this.context = context;conResolver = context.getContentResolver();}@SuppressWarnings("unchecked")public void testInsertSMS() {/** * 放一个解析xml文件的模块 */smsItems = this.getSmsItemsFromXml();Log.d("sqk", "after smsItems");for (SmsItem item : smsItems) {// 判断短信数据库中是否已包含该条短信,如果有,则不需要恢复Cursor cursor = conResolver.query(Uri.parse("content://sms"), new String[] { SmsField.DATE }, SmsField.DATE + "=?",new String[] { item.getDate() }, null);if (!cursor.moveToFirst()) {// 没有该条短信ContentValues values = new ContentValues();values.put(SmsField.ADDRESS, item.getAddress());// 如果是空字符串说明原来的值是null,所以这里还原为null存入数据库values.put(SmsField.PERSON, item.getPerson().equals("") ? null : item.getPerson());values.put(SmsField.DATE, item.getDate());values.put(SmsField.PROTOCOL, item.getProtocol().equals("") ? null : item.getProtocol());values.put(SmsField.READ, item.getRead());values.put(SmsField.STATUS, item.getStatus());values.put(SmsField.TYPE, item.getType());values.put(SmsField.REPLY_PATH_PRESENT, item.getReply_path_present().equals("") ? null : item.getReply_path_present());values.put(SmsField.BODY, item.getBody());values.put(SmsField.LOCKED, item.getLocked());values.put(SmsField.ERROR_CODE, item.getError_code());values.put(SmsField.SEEN, item.getSeen());conResolver.insert(Uri.parse("content://sms"), values);}cursor.close();}}//public void delete() {////conResolver.delete(Uri.parse("content://sms"), null, null);//}public List<SmsItem> getSmsItemsFromXml(){SmsItem smsItem = null;XmlPullParser parser = Xml.newPullParser();String absolutePath = Environment.getExternalStorageDirectory() + "/SMSBackup/message.xml";File file = new File(absolutePath);if (!file.exists()) {Looper.prepare();Toast.makeText(context, "message.xml短信备份文件不在sd卡中", 1).show();Looper.loop();//退出线程//return null;}try {FileInputStream fis = new FileInputStream(file);parser.setInput(fis, "UTF-8");int event = parser.getEventType();while (event != XmlPullParser.END_DOCUMENT) {switch (event) {case XmlPullParser.START_DOCUMENT:smsItems = new ArrayList<SmsItem>();break;case XmlPullParser.START_TAG: // 如果遇到开始标记,如<smsItems>,<smsItem>等if ("item".equals(parser.getName())) {smsItem = new SmsItem();smsItem.setAddress(parser.getAttributeValue(0));smsItem.setPerson(parser.getAttributeValue(1));smsItem.setDate(parser.getAttributeValue(2));smsItem.setProtocol(parser.getAttributeValue(3));smsItem.setRead(parser.getAttributeValue(4));smsItem.setStatus(parser.getAttributeValue(5));smsItem.setType(parser.getAttributeValue(6));smsItem.setReply_path_present(parser.getAttributeValue(7));smsItem.setBody(parser.getAttributeValue(8));smsItem.setLocked(parser.getAttributeValue(9));smsItem.setError_code(parser.getAttributeValue(10));smsItem.setSeen(parser.getAttributeValue(11));}break;case XmlPullParser.END_TAG:// 结束标记,如</smsItems>,</smsItem>等if ("item".equals(parser.getName())) {smsItems.add(smsItem);smsItem = null;}break;}event = parser.next();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blockLooper.prepare();Toast.makeText(context, "短信恢复出错", 1).show();Looper.loop();e.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch blockLooper.prepare();Toast.makeText(context, "短信恢复出错", 1).show();Looper.loop();e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blockLooper.prepare();Toast.makeText(context, "短信恢复出错", 1).show();Looper.loop();e.printStackTrace();}return smsItems;}}

3.辅助

package com.su.smsbackuprestore.operatesms;public class SmsField {public static final String ADDRESS = "address";public static final String PERSON = "person";public static final String DATE = "date";public static final String PROTOCOL = "protocol";public static final String READ = "read";public static final String STATUS = "status";public static final String TYPE = "type";public static final String REPLY_PATH_PRESENT = "reply_path_present";public static final String BODY = "body";public static final String LOCKED = "locked";public static final String ERROR_CODE = "error_code";public static final String SEEN = "seen";}

package com.su.smsbackuprestore.operatesms;public class SmsItem {String address;String person;String date;String protocol;String read;String status;String type;String reply_path_present;String body;String locked;String error_code;String seen;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getPerson() {return person;}public void setPerson(String person) {this.person = person;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getProtocol() {return protocol;}public void setProtocol(String protocol) {this.protocol = protocol;}public String getRead() {return read;}public void setRead(String read) {this.read = read;}public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getReply_path_present() {return reply_path_present;}public void setReply_path_present(String reply_path_present) {this.reply_path_present = reply_path_present;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public String getLocked() {return locked;}public void setLocked(String locked) {this.locked = locked;}public String getError_code() {return error_code;}public void setError_code(String error_code) {this.error_code = error_code;}public String getSeen() {return seen;}public void setSeen(String seen) {this.seen = seen;}}


更多相关文章

  1. android导出通讯录,通话记录,短信
  2. 基于Android的短信管理系统
  3. android使用Intent操作拨打号码发送短信
  4. android获取短信方法1
  5. android Fragment + FragmentStatePagerAdapter的恢复问题解决
  6. Android打电话&发短信
  7. Android(安卓)高通代码预制apk可卸载,恢复出厂设置apk可恢复 Andr
  8. 清单文件,测试,打电话和发短信应用
  9. Android短信发送流程之长短信发送(原)

随机推荐

  1. Android 开发之webview页面返回到最顶关
  2. Flash Android P Preview Image into Pix
  3. android多国语言包命名规则
  4. Android重启应用和重启手机
  5. 自定义录像机 没任何新意
  6. Android的dialog控制焦点导致其后Activit
  7. webview高度自适应图文详情
  8. Android 仿360恶意广告拦截扫描
  9. Android Name Rules 命名规则
  10. Android——简单对话框