Android實現支援進度條顯示的簡訊備份工具類_Android

來源:互聯網
上載者:User

使用內容提供者讀取簡訊內容,寫入XML檔案,進度條ProgressDialog更新備份進度。
新知識點:子線程如何在在不使用Handler的情況下更新UI

/**  *   進行簡訊備份的工具類,支援進度條顯示  * @author lian  *  */  public class SmsBackupUtils {   private static class Data{     int progress;   }      /**    *    * @param context    *   調用此工具類的Activity    * @param pd    *   顯示備份進度的進度條    */   public static void smsBackup(Activity context,final ProgressDialog pd){     Uri uri = Uri.parse("content://sms/");     ContentResolver cr = context.getContentResolver();          //取出簡訊     final Cursor cursor = cr.query(uri, new String[]{"address","date","body","type"}, null, null, null);          final int count = cursor.getCount();          final Data data = new Data();     data.progress = 0;          //儲存路徑     File file = new File(Environment.getExternalStorageDirectory(), "sms.xml");     try {       FileOutputStream fos = new FileOutputStream(file);       PrintWriter pw = new PrintWriter(fos);              //按照XML格式進行寫入       pw.println("<smses count='" + cursor.getCount() +"'>");              //在主線程中更新UI       context.runOnUiThread(new Runnable() {                  @Override         public void run() {           // TODO Auto-generated method stub           pd.setMax(count);           pd.show();         }       });              //寫入XML檔案       while(cursor.moveToNext()){         data.progress ++;         String address = cursor.getString(0);         String date = cursor.getString(1);         String body = cursor.getString(2);         String type = cursor.getString(3);                  //SystemClock.sleep(150);         pw.println("<sms>");         pw.println("<address>"+ address +"</address>");         pw.println("<date>"+ date +"</date>");         pw.println("<body>"+ body +"</body>");         pw.println("<type>"+ type +"</type>");         pw.println("</sms>");                  context.runOnUiThread(new Runnable() {                      @Override           public void run() {             // TODO Auto-generated method stub             pd.setProgress(data.progress);           }         });                }       pw.println("</smses>");       pw.flush();       pw.close();       cursor.close();       //備份完成,關閉進度條       context.runOnUiThread(new Runnable() {         @Override         public void run() {           // TODO Auto-generated method stub           pd.dismiss();         }       });     } catch (Exception e) {       // TODO Auto-generated catch block       e.printStackTrace();     }             } } 

調用

pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  SmsBackupUtils.smsBackup(SuperToolActivity.this, pd); 

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.