Android implementation to support the progress bar display of SMS Backup tool class _android

Source: Internet
Author: User
Tags stub

The

uses content providers to read SMS content, write XML files, progress bar ProgressDialog update backup progress.
New knowledge Point: How Child threads update the UI without using handler

/** * SMS Backup tool class, support progress bar display * @author Lian * * */public class Smsbackuputils {private static Class data{ 
  int progress;  /** * * @param context * Call activity of this tool class * @param PD * Show progress bar for backup progress/public static 
    void Smsbackup (Activity context,final progressdialog PD) {uri uri = uri.parse ("content://sms/"); 
     
    Contentresolver CR = Context.getcontentresolver (); 
     
    Take out the text message 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; 
    Store path file = new file (Environment.getexternalstoragedirectory (), "Sms.xml"); 
      try {fileoutputstream fos = new FileOutputStream (file); 
       
      PrintWriter pw = new PrintWriter (FOS); 
       
      Write in XML format pw.println ("<smses count= '" + cursor.getcount () + "' >"); 
 Updating the UI in the main thread     Context.runonuithread (New Runnable () {@Override public void run () {//TODO A 
          uto-generated Method Stub Pd.setmax (count); 
        Pd.show (); 
       
      } 
      }); 
        Write XML file 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 () {//TOD O Auto-generated method Stub pd.setprogress (data.progress); 
         
      } 
        }); 
      } pw.println ("</smses>"); 
      Pw.flush (); 
      Pw.close (); 
      Cursor.close (); Backup complete, close progress bar context.runonuithread (new Runnable () {@Override public void run () {//T 
        Odo auto-generated Method Stub Pd.dismiss (); 
    } 
      }); 
    catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); 
 } 
     
     
  } 
}

Call

PD = new ProgressDialog (this); 
Pd.setprogressstyle (progressdialog.style_horizontal); 
 

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.