Trainer small project (5) security guard _ using content providers for text message backup correction version, trainer providers

Source: Internet
Author: User

Trainer small project (5) security guard _ using content providers for text message backup correction version, trainer providers

The text message backup class is basically finished in the last section. Iterative code development is required. Code reconstruction and decoupling are required after the development.


Knowledge points that can be learned

① Application of interfaces

② A simple ui thread uses runOnUiThread


In the previous section, it is a headache for a project team if the company boss has frequent UI requirements, because basically one person is responsible for the UI and one person is responsible for the business class. Therefore, we need to transform Smsutils for decoupling.


This is because the UI is frequently changed in the class. When the backup is started, we set the maximum progress and increase the progress during the backup process. Therefore, we can extract the frequently changed ones and define them as interfaces, let the user select the value to pass.


① Application of interfaces

/*** Backup SMS callback interface */public interface BackUpCallBack, set the maximum value of the Progress ** @ param max * total progress */public void beforeBackup (int max);/*** during the backup process, add progress ** @ param progress * current progress */public void onSmsBackup (int progress );}

// Pd. setMax (max );
CallBack. beforeBackup (max );

When the tool class goes to this step, because the abstract method of the interface is written by the caller, it finally executes

pd.setProgress(progress);

And so on

//            pd.setProgress(process);            callBack.onSmsBackup(process);
At this point, it was indeed called.

pd.setMax(max);
At this time, the progressbar will appear. This decouples


② A simple ui thread uses runOnUiThread

Because many text messages may be sent to users during text message backup, A subthread is enabled for update, then we need to use Toast. At this time, we simply use UI update to use Toast.


RunOnUiThread



RunOnUiThread (new Runnable () {@ Overridepublic void run () {Toast. makeText (SmsActivity. this, "backup successful", 0). show ();}});



SmsUtils. java backup text message Tool

Package com. example. darkbutton. utils; import java. io. file; import java. io. fileOutputStream; import org. xmlpull. v1.XmlSerializer; import android. app. progressDialog; import android. content. contentResolver; import android. content. context; import android. database. cursor; import android.net. uri; import android. OS. environment; import android. util. xml;/*** text message tool class **/public class SmsUtils {/*** backup SMS callback interface */public interface BackUpCallBack {/*** when backup is started, set the maximum value of the Progress ** @ param max * total progress */public void beforeBackup (int max);/*** during the backup process, add progress ** @ param progress * current progress */public void onSmsBackup (int progress);} public static void backupSms (Context context, BackUpCallBack callBack) throws Exception {// ProgressDialog pd = new ProgressDialog (context); // pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); // pd. setMessage ("backing up data"); // pd. show (); File file = new File (Environment. getExternalStorageDirectory (), "backup. xml "); FileOutputStream fos = new FileOutputStream (file); ContentResolver resolver = context. getContentResolver (); Uri uri = Uri. parse ("content: // sms/"); Cursor cursor = resolver. query (uri, new String [] {"body", "address", "type", "date"}, null); // when starting backup, set the maximum value of the progress bar int max = cursor. getCount (); // pd. setMax (max); callBack. beforeBackup (max); // read your text message one by one and write it to the file in a certain format: XmlSerializer serializer = Xml. newSerializer (); // initialize the generator serializer. setOutput (fos, "UTF-8"); serializer. startDocument ("UTF-8", true); serializer. startTag (null, "smss"); serializer. attribute (null, "max", max + ""); int process = 0; while (cursor. moveToNext () {Thread. sleep (500); String body = cursor. getString (0); String address = cursor. getString (1); String type = cursor. getString (2); String date = cursor. getString (3); serializer. startTag (null, "sms"); serializer. startTag (null, "body"); serializer. text (body); serializer. endTag (null, "body"); serializer. startTag (null, "address"); serializer. text (address); serializer. endTag (null, "address"); serializer. startTag (null, "type"); serializer. text (type); serializer. endTag (null, "type"); serializer. startTag (null, "date"); serializer. text (date); serializer. endTag (null, "date"); serializer. endTag (null, "sms"); // Add progress process ++ during the backup process; // pb. setProgress (process); // pd. setProgress (process); callBack. onSmsBackup (process);} cursor. close (); serializer. endTag (null, "smss"); serializer. endDocument (); fos. close (); // pd. dismiss ();}}

SmsActivity

Package com. example. darkbutton; import com. example. darkbutton. utils. smsUtils; import com. example. darkbutton. utils. smsUtils. backUpCallBack; import android. app. activity; import android. app. progressDialog; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class SmsActivity extends Activity implements OnClickListener {private Button button1; private ProgressDialog pd; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. sms_activity); button1 = (Button) findViewById (R. id. button1); button1.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {pd = new ProgressDialog (this); pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); pd. setMessage ("backing up SMS"); pd. show (); new Thread () {@ Overridepublic void run () {try {SmsUtils. backupSms (SmsActivity. this, new BackUpCallBack () {@ Overridepublic void onSmsBackup (int progress) {pd. setProgress (progress) ;}@ Overridepublic void beforeBackup (int max) {pd. setMax (max) ;}}); runOnUiThread (new Runnable () {@ Overridepublic void run () {Toast. makeText (SmsActivity. this, "backup successful", 0 ). show () ;}) ;}catch (Exception e) {e. printStackTrace (); runOnUiThread (new Runnable () {@ Overridepublic void run () {Toast. makeText (SmsActivity. this, "backup failed", 0 ). show () ;}) ;}finally {pd. dismiss ();}};}. start ();}}

Because the Code has some other things in my test demo, the source code is not put. Remember to add permissions.

     <uses-permission android:name="android.permission.READ_SMS"/>     <uses-permission android:name="android.permission.WRITE_SMS"/>


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.