Android mobile guard 9 -- text message backup, android9 --

Source: Internet
Author: User

Android mobile guard 9 -- text message backup, android9 --

 

AToolActivity. java

1 protected void showSmsBackUpDialog () {2 // 1. Create a dialog box with a progress bar. 3 final ProgressDialog ssssdialog = new progressDialog (this); 4 ProgressDialog. setIcon (R. drawable. ic_launcher); 5 progressDialog. setTitle ("SMS backup"); 6 // 2, specify the progress bar style as level 7 progressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); 8 // 3, displays the progress bar 9 progressDialog. show (); 10 // 4. Call the backup text message method to run 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 // depends on the developer. Use the dialog box or the progress bar 19 progressDialog. setProgress (index); 20 pb_bar.setProgress (index); 21} 22 23 @ Override24 public void setMax (int max) {25 // The developer determines whether to use the dialog box or the progress bar 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 // backup text message method 16 // ProgressDialog pd original Dialog Box 17 // now change to progress bar 18 19 // A, pass A dialog box where the progress bar is located 20 // B, and pass A progress bar 21 22 public static void backup (Context ctx, String path, callBack callBack) {23 FileOutputStream fos = null; 24 Cursor cursor = null; 25 try {26 // the target object context, backup folder path, the dialog box object where the progress bar is located is used to update the progress during the backup process 27 // 1, and get the File 28 file File = new File (path); 29 // 2, get the content parser and get the data 30 cursor = ctx in the SMS database. getContentResolver (). query (Uri. parse ("content: // sms/"), 31 ne W String [] {"address", "date", "type", "body"}, null); 32 // 3, file output stream 33 fos = new FileOutputStream (file); 34 // 4. serialize the data read from the database and place it in xml 35 XmlSerializer newSerializer = Xml. newSerializer (); 36 // 5, set the next xml to 37 newSerializer. setOutput (fos, "UTF-8"); 38 // DTD (xml specification) 39 newSerializer. startDocument ("UTF-8", true); 40 41 newSerializer. startTag (null, "smss"); 42 43 // 6, specify 44 for the total number of backup messages // A if Dialog box, specify the total number of progress bars in the dialog box 45 // B if the progress bar is passed in, specify the total number of progress bars 46 // pd. setMax (cursor. getCount (); 47 48 if (callBack! = Null) {49 callBack. setMax (cursor. getCount (); 50} 51 52 // 7, reads data from each row in the database and writes it to 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 (nu Ll, "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, each cycle needs to overlay the progress bar with 75 index ++; 76 Thread. sleep (500); 77 // ProgressDialog you can update the corresponding inputs in the Child thread Degree bar change 78 79 // A if the dialog box is passed in, specify the current percentage of the progress bar in the dialog box 80 // B if the progress bar is passed in, current percentage of the specified progress bar 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 // callback 103 // 1. define an interface 104 // 2, and define the unimplemented business logic methods in the interface (set the total number of SMS messages, and update the SMS percentage during the backup process) 105 // 3. when an object of the class that implements this interface is passed (to the tool class of the backup SMS), the Implementation class of the interface must implement the appeal two implementation methods (it determines the use of the dialog box, or progress bar) 106 // 4. get the passed object, in the appropriate place (set the total number, set the percentage) call method 107 public interface CallBack {108 // set the total number of SMS messages to the implementation method (you can choose to use the dialog box by yourself. setMax (max) still uses the progress bar. setMax (max) 109 public void setMax (int max); 110 // update the SMS percentage during the backup process (the dialog box is determined by yourself. setProgress (max) still uses the progress bar. setProgress (max) 111 public void setProgress (int index); 112} 113}
SmsBackUp

 

Related Article

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.