歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 短信的備份與恢復(導入導出)

最近在做一個Android 備份軟件,剛剛把短信的備份與恢復做完。短信的備份主要是對數據庫mmssms.db的sms表進行數據的導出,當然這裡是跨應用的,所以要使用contendResolver來訪問,最後把數據導出成xml文件。恢復時再對xml文件解析。備份時注意,不要備份thread_id字段,否則恢復時,手機中查看不到恢復的短信。

源代碼下載在Linux公社的1號FTP服務器裡,下載地址:

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

用戶名:www.linuxidc.com

密碼:www.muu.cc

在 2011年LinuxIDC.com\10月\10月\Android 短信的備份與恢復(導入導出)

下載方法見 http://www.linuxidc.net/thread-1187-1-1.html

短信導出源碼:

  1. package cn.sqk.sms;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.PrintWriter;  
  7. import java.text.SimpleDateFormat;  
  8. import java.util.Date;  
  9.   
  10. import org.dom4j.Document;  
  11. import org.dom4j.DocumentHelper;  
  12. import org.dom4j.Element;  
  13. import org.xmlpull.v1.XmlSerializer;  
  14.   
  15. import cn.sqk.constants.SmsField;  
  16.   
  17. import android.content.ContentResolver;  
  18. import android.content.Context;  
  19. import android.database.Cursor;  
  20. import android.database.sqlite.SQLiteException;  
  21. import android.net.Uri;  
  22. import android.os.Environment;  
  23. import android.util.Log;  
  24. import android.util.Xml;  
  25. import android.widget.Toast;  
  26.   
  27. public class ExportSmsXml {  
  28.     Context context;  
  29.     public static final String SMS_URI_ALL = "content://sms/";  
  30.     private FileOutputStream outStream = null;  
  31.     private XmlSerializer serializer;  
  32.   
  33.     public ExportSmsXml(Context context) {  
  34.         this.context = context;  
  35.     }  
  36.   
  37.     public void xmlStart() {  
  38.   
  39.         String path = Environment.getExternalStorageDirectory().getPath() + "/exportSms";  
  40.         File file = new File(path);  
  41.         if (!file.exists()) {  
  42.             file.mkdirs();  
  43.         }  
  44.         File file2 = new File(path, "message.xml");  
  45.         try {  
  46.             outStream = new FileOutputStream(file2);  
  47.             serializer = Xml.newSerializer();  
  48.             serializer.setOutput(outStream, "UTF-8");  
  49.             serializer.startDocument("UTF-8"true);  
  50.             serializer.startTag(null"sms");  
  51.               
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56.   
  57.     public boolean createXml() throws Exception {  
  58.   
  59.         this.xmlStart();  
  60.         Cursor cursor = null;  
  61.         try {  
  62.             ContentResolver conResolver = context.getContentResolver();  
  63.             String[] projection = new String[] { SmsField.ADDRESS, SmsField.PERSON, SmsField.DATE, SmsField.PROTOCOL,   
  64.                                                 SmsField.READ, SmsField.STATUS, SmsField.TYPE, SmsField.REPLY_PATH_PRESENT,  
  65.                                                 SmsField.BODY,SmsField.LOCKED,SmsField.ERROR_CODE, SmsField.SEEN }; // type=1是收件箱,==2是發件箱;read=0表示未讀,read=1表示讀過,seen=0表示未讀,seen=1表示讀過   
  66.             Uri uri = Uri.parse(SMS_URI_ALL);  
  67.             cursor = conResolver.query(uri, projection, nullnull"_id asc");  
  68.             if (cursor.moveToFirst()) {  
  69.                 // 查看數據庫sms表得知 subject和service_center始終是null所以這裡就不獲取它們的數據了。   
  70.                 String address;  
  71.                 String person;  
  72.                 String date;  
  73.                 String protocol;  
  74.                 String read;  
  75.                 String status;  
  76.                 String type;  
  77.                 String reply_path_present;  
  78.                 String body;  
  79.                 String locked;  
  80.                 String error_code;  
  81.                 String seen;  
  82.                 do {  
  83.                     // 如果address == null,xml文件中是不會生成該屬性的,為了保證解析時,屬性能夠根據索引一一對應,必須要保證所有的item標記的屬性數量和順序是一致的   
  84.                     address = cursor.getString(cursor.getColumnIndex(SmsField.ADDRESS));  
  85.                     if (address == null) {  
  86.                         address = "";  
  87.                     }  
  88.                     person = cursor.getString(cursor.getColumnIndex(SmsField.PERSON));  
  89.                     if (person == null) {  
  90.                         person = "";  
  91.                     }  
  92.                     date = cursor.getString(cursor.getColumnIndex(SmsField.DATE));  
  93.                     if (date == null) {  
  94.                         date = "";  
  95.                     }  
  96.                     protocol = cursor.getString(cursor.getColumnIndex(SmsField.PROTOCOL));  
  97.                     if (protocol == null) {// 為了便於xml解析   
  98.                         protocol = "";  
  99.                     }  
  100.                     read = cursor.getString(cursor.getColumnIndex(SmsField.READ));  
  101.                     if (read == null) {  
  102.                         read = "";  
  103.                     }  
  104.                     status = cursor.getString(cursor.getColumnIndex(SmsField.STATUS));  
  105.                     if (status == null) {  
  106.                         status = "";  
  107.                     }  
  108.                     type = cursor.getString(cursor.getColumnIndex(SmsField.TYPE));  
  109.                     if (type == null) {  
  110.                         type = "";  
  111.                     }  
  112.                     reply_path_present = cursor.getString(cursor.getColumnIndex(SmsField.REPLY_PATH_PRESENT));  
  113.                     if (reply_path_present == null) {// 為了便於XML解析   
  114.                         reply_path_present = "";  
  115.                     }  
  116.                     body = cursor.getString(cursor.getColumnIndex(SmsField.BODY));  
  117.                     if (body == null) {  
  118.                         body = "";  
  119.                     }  
  120.                     locked = cursor.getString(cursor.getColumnIndex(SmsField.LOCKED));  
  121.                     if (locked == null) {  
  122.                         locked = "";  
  123.                     }  
  124.                     error_code = cursor.getString(cursor.getColumnIndex(SmsField.ERROR_CODE));  
  125.                     if (error_code == null) {  
  126.                         error_code = "";  
  127.                     }  
  128.                     seen = cursor.getString(cursor.getColumnIndex(SmsField.SEEN));  
  129.                     if (seen == null) {  
  130.                         seen = "";  
  131.                     }  
  132.                     // 生成xml子標記   
  133.                     // 開始標記   
  134.                     serializer.startTag(null"item");  
  135.                     // 加入屬性   
  136.                     serializer.attribute(null, SmsField.ADDRESS, address);  
  137.                     serializer.attribute(null, SmsField.PERSON, person);  
  138.                     serializer.attribute(null, SmsField.DATE, date);  
  139.                     serializer.attribute(null, SmsField.PROTOCOL, protocol);  
  140.                     serializer.attribute(null, SmsField.READ, read);  
  141.                     serializer.attribute(null, SmsField.STATUS, status);  
  142.                     serializer.attribute(null, SmsField.TYPE, type);  
  143.                     serializer.attribute(null, SmsField.REPLY_PATH_PRESENT, reply_path_present);  
  144.                     serializer.attribute(null, SmsField.BODY, body);  
  145.                     serializer.attribute(null, SmsField.LOCKED, locked);  
  146.                     serializer.attribute(null, SmsField.ERROR_CODE, error_code);  
  147.                     serializer.attribute(null, SmsField.SEEN, seen);  
  148.                     // 結束標記   
  149.                     serializer.endTag(null"item");  
  150.   
  151.                 } while (cursor.moveToNext());  
  152.             } else {  
  153.   
  154.                 return false;  
  155.             }  
  156.   
  157.         } catch (SQLiteException ex) {  
  158.             ex.printStackTrace();  
  159.             Log.d("SQLiteException:", ex.getMessage());  
  160.         }finally {  
  161.             if(cursor != null) {  
  162.                 cursor.close();//手動關閉cursor,及時回收   
  163.             }  
  164.         }  
  165.         serializer.endTag(null"sms");  
  166.         serializer.endDocument();  
  167.         outStream.flush();  
  168.         outStream.close();  
  169.         return true;  
  170.     }  
  171. }  
Copyright © Linux教程網 All Rights Reserved