Android 手機衛士9--簡訊備份,android9--

來源:互聯網
上載者:User

Android 手機衛士9--簡訊備份,android9--

 

AToolActivity.java

 1 protected void showSmsBackUpDialog() { 2     //1,建立一個帶進度條的對話方塊 3     final ProgressDialog progressDialog = new ProgressDialog(this); 4     progressDialog.setIcon(R.drawable.ic_launcher); 5     progressDialog.setTitle("簡訊備份"); 6     //2,指定進度條的樣式為水平 7     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 8     //3,展示進度條 9     progressDialog.show();10     //4,直接調用備份簡訊方法即可11     new Thread(){12         @Override13         public void run() {14             String path = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"sms74.xml";15             SmsBackUp.backup(getApplicationContext(), path, new CallBack() {16                 @Override17                 public void setProgress(int index) {18                     //由開發人員自己決定,使用對話方塊還是進度條19                     progressDialog.setProgress(index);20                     pb_bar.setProgress(index);21                 }22                 23                 @Override24                 public void setMax(int max) {25                     //由開發人員自己決定,使用對話方塊還是進度條26                     progressDialog.setMax(max);27                     pb_bar.setMax(max);28                 }29             });30             31             progressDialog.dismiss();32         }33     }.start();34 }

SmsBackUp.java

  1 package com.itheima.mobilesafe74.engine;  2   3 import java.io.File;  4 import java.io.FileOutputStream;  5 import java.io.IOException;  6 import org.xmlpull.v1.XmlSerializer;  7 import android.content.Context;  8 import android.database.Cursor;  9 import android.net.Uri; 10 import android.util.Xml; 11  12 public class SmsBackUp { 13     private static int index = 0; 14      15     //備份簡訊方法 16     //ProgressDialog pd    原來的對話方塊 17     //現在改為進度條 18      19     //A,傳遞一個進度條所在的對話方塊 20     //B,傳遞一個進度條 21      22     public static void backup(Context ctx,String path,CallBack callBack) { 23         FileOutputStream fos = null; 24         Cursor cursor = null; 25         try { 26             //需要用到的物件內容環境,備份檔案夾路徑,進度條所在的對話方塊對象用於備份過程中進度的更新 27             //1,擷取備份簡訊寫入的檔案 28             File file = new File(path); 29             //2,擷取內容解析器,擷取簡訊資料庫中資料 30             cursor = ctx.getContentResolver().query(Uri.parse("content://sms/"),  31                     new String[]{"address","date","type","body"}, null, null, null); 32             //3,檔案相應的輸出資料流 33             fos = new FileOutputStream(file); 34             //4,序列化資料庫中讀取的資料,放置到xml中 35             XmlSerializer newSerializer = Xml.newSerializer(); 36             //5,給次xml做相應設定 37             newSerializer.setOutput(fos, "utf-8"); 38             //DTD(xml規範) 39             newSerializer.startDocument("utf-8", true); 40              41             newSerializer.startTag(null, "smss"); 42              43             //6,備份簡訊總數指定 44             //A 如果傳遞進來的是對話方塊,指定對話方塊進度條的總數 45             //B    如果傳遞進來的是進度條,指定進度條的總數 46 //            pd.setMax(cursor.getCount()); 47              48             if(callBack!=null){ 49                 callBack.setMax(cursor.getCount()); 50             } 51              52             //7,讀取資料庫中的每一行的資料寫入到xml中 53             while(cursor.moveToNext()){ 54                 newSerializer.startTag(null, "sms"); 55                  56                 newSerializer.startTag(null, "address"); 57                 newSerializer.text(cursor.getString(0)); 58                 newSerializer.endTag(null, "address"); 59                  60                 newSerializer.startTag(null, "date"); 61                 newSerializer.text(cursor.getString(1)); 62                 newSerializer.endTag(null, "date"); 63                  64                 newSerializer.startTag(null, "type"); 65                 newSerializer.text(cursor.getString(2)); 66                 newSerializer.endTag(null, "type"); 67                  68                 newSerializer.startTag(null, "body"); 69                 newSerializer.text(cursor.getString(3)); 70                 newSerializer.endTag(null, "body"); 71                  72                 newSerializer.endTag(null, "sms"); 73                  74                 //8,每迴圈一次就需要去讓進度條疊加 75                 index++; 76                 Thread.sleep(500); 77                 //ProgressDialog可以在子線程中更新相應的進度條的改變 78                  79                 //A 如果傳遞進來的是對話方塊,指定對話方塊進度條的當前百分比 80                 //B    如果傳遞進來的是進度條,指定進度條的當前百分比 81 //                pd.setProgress(index); 82                  83                 if(callBack!=null){ 84                     callBack.setProgress(index); 85                 } 86             } 87             newSerializer.endTag(null, "smss"); 88             newSerializer.endDocument(); 89         } catch (Exception e) { 90             e.printStackTrace(); 91         }finally{ 92             try { 93                 if(cursor!=null && fos!=null){ 94                     cursor.close(); 95                     fos.close(); 96                 } 97             } catch (IOException e) { 98                 e.printStackTrace(); 99             }100         }101     }102     //回調103     //1.定義一個介面104     //2,定義介面中未實現的商務邏輯方法(簡訊總數設定,備份過程中簡訊百分比更新)105     //3.傳遞一個實現了此介面的類的對象(至備份簡訊的工具類中),介面的實作類別,一定實現了上訴兩個為實現方法(就決定了使用對話方塊,還是進度條)106     //4.擷取傳遞進來的對象,在合適的地方(設定總數,設定百分比的地方)做方法的調用107     public interface CallBack{108         //簡訊總數設定為實現方法(由自己決定是用    對話方塊.setMax(max) 還是用    進度條.setMax(max))109         public void setMax(int max);110         //備份過程中簡訊百分比更新(由自己決定是用    對話方塊.setProgress(max) 還是用    進度條.setProgress(max))111         public void setProgress(int index);112     }113 }
SmsBackUp

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.