Android 短信的备份与恢复(导入导出)

最近在做一个Android 备份软件,刚刚把短信的备份与恢复做完。短信的备份主要是对数据库mmssms.db的sms表进行数据的导出,当然这里是跨应用的,所以要使用contendResolver来访问,最后把数据导出成xml文件。恢复时再对xml文件解析。备份时注意,不要备份thread_id字段,否则恢复时,手机中查看不到恢复的短信。

源代码下载在Linux公社的1号FTP服务器里,下载地址:

FTP地址:ftp://www.linuxidc.com

用户名:

密码:

在 2011年LinuxIDC.com\10月\10月\Android 短信的备份与恢复(导入导出)

下载方法见

短信导出源码:

package cn.sqk.sms;      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 cn.sqk.constants.SmsField;      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() + "/exportSms";           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, nullnull"_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;       }   }  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwywdf.html