Okay. The last time we talked about the backup of the text message, since there is a backup, there must be a restoration. Today we will do the restoration.
In fact, it is very easy to parse the xml of the last backup and then write it back to the database.
Now let's start writing code. First add a method in the previous com. xiaobin. security. engine. SmsService.
Package com. xiaobin. security. engine; import java. io. file; import java. io. fileInputStream; import java. util. arrayList; import java. util. list; import org. xmlpull. v1.XmlPullParser; import android. app. progressDialog; import android. content. contentResolver; import android. content. contentValues; import android. content. context; import android. database. cursor; import android.net. uri; import android. util. xml; import com. Xiaobin. security. domain. smsInfo; public class SmsService {private Context context; public SmsService (Context context) {this. context = context;} public List <SmsInfo> getSmsInfo () {List <SmsInfo> infos = new ArrayList <SmsInfo> (); ContentResolver resolver = context. getContentResolver (); Uri uri = Uri. parse ("content: // sms/"); Cursor cursor = resolver. query (uri, new String [] {"_ id", "address", "date", "type "," Body "}, null, null," date desc "); SmsInfo info; while (cursor. moveToNext () {info = new SmsInfo (); String id = cursor. getString (0); String address = cursor. getString (1); String date = cursor. getString (2); int type = cursor. getInt (3); String body = cursor. getString (4); info. setId (id); info. setAddress (address); info. setDate (date); info. setType (type); info. setBody (body); infos. add (info);} cursor. close (); return inf OS;} // restore the text message path to public void restore (String path, ProgressDialog pd) throws Exception {File file = new File (path ); fileInputStream FCM = new FileInputStream (file); XmlPullParser parser = Xml. newPullParser (); ContentValues values = null; parser. setInput (FCM, "UTF-8"); int type = parser. getEventType (); int index = 0; while (type! = XmlPullParser. END_DOCUMENT) {switch (type) {case XmlPullParser. START_TAG: if ("count ". equals (parser. getName () {int count = Integer. parseInt (parser. nextText (); pd. setMax (count);} if ("sms ". equals (parser. getName () {values = new ContentValues ();} else if ("address ". equals (parser. getName () {values. put ("address", parser. nextText ();} else if ("date ". equals (parser. getName () {values. put ("date", parser. nextText ();} else if ("type ". equals (parser. getName () {values. put ("type", parser. nextText ();} else if ("body ". equals (parser. getName () {values. put ("body", parser. nextText ();} break; case XmlPullParser. END_TAG: if ("sms ". equals (parser. getName () {ContentResolver resolver = context. getContentResolver (); resolver. insert (Uri. parse ("content: // sms/"), values); values = null; index ++; pd. setProgress (index);} break; default: break;} type = parser. next ();}}}
It is very important to restore the text message. If there is a problem with the recovery, the data may be lost. Therefore, we added a dialog box so that users do not need to cancel the operation, until recovery is complete
Now we can write down the method for restoring the text message. Now we will go back to the com. xiaobin. security. ui. AToolActivity class. There is still a way to add the corresponding event for the text message recovery entry.
Because SMS recovery is a very time-consuming operation, we start a thread to run it.
Com. xiaobin. security. ui. AToolActivity
Package com. xiaobin. security. ui; import java. io. file; import android. annotation. suppressLint; import android. app. activity; import android. app. alertDialog; import android. app. progressDialog; import android. content. context; import android. content. dialogInterface; import android. content. intent; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. graphics. colo R; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. textView; import android. widget. toast; import com. x Iaobin. security. r; import com. xiaobin. security. engine. downloadTask; import com. xiaobin. security. engine. smsService; import com. xiaobin. security. service. addressService; import com. xiaobin. security. service. backupSmsService; public class AToolActivity extends Activity implements OnClickListener {private static final int ERROR = 0; private static final int SUCCESS = 1; private TextView TV _atool_query; private T ExtView detail; private CheckBox cb_atool_state; private TextView detail; private TextView TV _atool_change_location; private TextView detail; private Intent serviceIntent; private ProgressDialog pd; private SharedPreferences sp; @ SuppressLint ("HandlerLeak") private Handler handler = New Handler () {public void handleMessage (Message msg) {switch (msg. what) {case ERROR: Toast. makeText (AToolActivity. this, "failed to download database. Please check the network! ", Toast. LENGTH_SHORT). show (); break; case SUCCESS: Toast. makeText (AToolActivity. this," the database has been downloaded successfully! ", Toast. LENGTH_SHORT ). show (); break; default: break ;}};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. atool); sp = getSharedPreferences ("config", Context. MODE_PRIVATE); TV _atool_query = (TextView) findViewById (R. id. TV _atool_query); TV _atool_query.setOnClickListener (this); TV _atool_select_bg = (TextView) findViewById (R. id. TV _atool _ Select_bg); TV _atool_select_bg.setOnClickListener (this); TV _atool_change_location = (TextView) findViewById (R. id. TV _atool_change_location); TV _atool_change_location.setOnClickListener (this); TV _atool_number_security = (TextView) findViewById (R. id. TV _atool_number_security); TV _atool_number_security.setOnClickListener (this); TV _atool_sms_backup = (TextView) findViewById (R. id. TV _atool_sms_backup); TV _atool _ Sms_backup.setOnClickListener (this); TV _atool_sms_restore = (TextView) findViewById (R. id. TV _atool_sms_restore); TV _atool_sms_restore.setOnClickListener (this); TV _atool_number_service_state = (TextView) findViewById (R. id. TV _atool_number_service_state); cb_atool_state = (CheckBox) findViewById (R. id. cb_atool_state); serviceIntent = new Intent (this, AddressService. class); cb_atool_state.setOnCheckedChangeLi Stener (new OnCheckedChangeListener () {@ Overridepublic void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked) {startService (serviceIntent); identifier (Color. BLACK); TV _atool_number_service_state.setText ("");} else {stopService (serviceIntent); TV _atool_number_service_state.setTextColor (Color. RED); TV _atool_number_service_state.setText (R. stri Ng. number_service_state) ;}}}) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. TV _atool_query: query (); break; case R. id. TV _atool_select_bg: selectStyle (); break; case R. id. TV _atool_change_location: Intent intent = new Intent (this, DragViewActivity. class); startActivity (intent); break; case R. id. TV _atool_number_security: Intent I = new Intent (this, NumberSecurityActivity. class); start Activity (I); break; case R. id. TV _atool_sms_backup: Intent backupIntent = new Intent (this, BackupSmsService. class); startService (backupIntent); break; case R. id. TV _atool_sms_restore: restore (); break; default: break;} private void query () {if (isDBExist () {Intent intent = new Intent (this, QueryNumberActivity. class); startActivity (intent);} else {// the user is prompted to download the database pd = new ProgressDialog (this); pd. setMessage ("downloading data... Library... "); pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); pd. setCancelable (false); pd. show (); new Thread () {public void run () {String path = getResources (). getString (R. string. serverdb); File dir = new File (Environment. getExternalStorageDirectory (), "/security/db"); if (! Dir. exists () {dir. mkdirs ();} String dbPath = Environment. getExternalStorageDirectory () + "/security/db/data. db "; try {// this class has been written during apk update. Now we can use it directly to DownloadTask. getFile (path, dbPath, pd); pd. dismiss ();} catch (Exception e) {e. printStackTrace (); pd. dismiss (); Message message = new Message (); message. what = ERROR; handler. sendMessage (message );}};}. start () ;}} private boolean isDBExist () {if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {File file = new File (Environment. getExternalStorageDirectory () + "/security/db/data. db "); if (file. exists () {return true ;}} return false;} private void selectStyle () {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle (""); String [] items = new String [] {"Translucent", "active orange", "apple green", "peacock blue", "metallic gray "}; builder. setSingleChoiceItems (items, 0, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Editor editor = sp. edit (); editor. putInt ("background", which); editor. commit () ;}}); builder. setPositiveButton (android. r. string. OK, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {}}); builder. create (). show ();} private void restore () {final ProgressDialog pd = new ProgressDialog (this); pd. setTitle ("Restore SMS"); pd. setMessage ("restoring SMS... "); pd. setCancelable (false); pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); pd. show (); final SmsService smsService = new SmsService (this); new Thread () {public void run () {try {smsService. restore (Environment. getExternalStorageDirectory () + "/security/backup/smsbackup. xml ", pd); pd. dismiss (); logoff. prepare (); // create a LooperToast. makeText (getApplicationContext (), "restored successfully", Toast. LENGTH_SHORT ). show (); logoff. loop (); // round-robin logoff} catch (Exception e) {e. printStackTrace (); logoff. prepare (); // create a LooperToast. makeText (getApplicationContext (), "Restore failed", Toast. LENGTH_SHORT ). show (); logoff. loop (); // round-robin logoff }}}. start ();}}
Okay, the operation to restore the text message is now complete.
Download source code today