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.