Back up SMS in Android to restore SMS

Source: Internet
Author: User

Back up SMS in Android to restore SMS
Backup SMS

Read text messages from your mobile phone, and back up them as xml files and save them to the SD card.

The xml file has only one button control and no xml code is provided.

 

Import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import org. xmlpull. v1.XmlSerializer; import android. app. activity; import android. content. contentResolver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. util. xml; import android. view. view; import android. view. view. OnClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private Button bt_readsms;/*** text message backup File object */private File smsBackUpFile; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bt_readsms = (Button) findViewById (R. id. bt_readsms); bt_readsms.setOnCl IckListener (new OnClickListener () {@ Overridepublic void onClick (View v) {try {// create a File object smsBackUpFile = new File (Environment. getExternalStorageDirectory (), "sms. xml "); // create an xml file generator. XmlSerializer serializer = Xml. newSerializer (); // complete the serializer initialization. FileOutputStream OS = new FileOutputStream (smsBackUpFile); serializer. setOutput (OS, "UTF-8"); // content provider. // The parser who obtains the content of a database is ContentResolver resolver = getContentResolver (); // Cursor (result set) cursor Cursor = resolver. query (Uri. parse ("content: // sms"), new String [] {"address", "date", "type", "body"}, null ); // generate the xml file header serializer. startDocument ("UTF-8", true); serializer. startTag (null, "smss"); while (cursor. moveToNext () {serializer. startTag (null, "sms"); String address = cursor. getString (0); String date = cursor. getString (1); String type = cursor. getString (2); String body = cursor. getString (3); serializer. startTag (null, "address"); System. out. println ("SMS address:" + address); serializer. text (address); serializer. endTag (null, "address"); serializer. startTag (null, "date"); System. out. println ("SMS time:" + date); serializer. text (date); serializer. endTag (null, "date"); serializer. startTag (null, "body"); System. out. println ("SMS content:" + body); serializer. text (body); serializer. endTag (null, "body"); serializer. startTag (null, "type"); System. out. println ("SMS type:" + type); serializer. text (type); serializer. endTag (null, "type"); serializer. endTag (null, "sms");} cursor. close (); serializer. endTag (null, "smss"); serializer. endDocument (); OS. close (); Toast. makeText (getApplicationContext (), "backup completed", 1 ). show ();} catch (Exception e) {e. printStackTrace ();}}});}}

 

Restore SMS

 

Use the code program to send text messages to the mobile phone. After 10 seconds, the mobile phone can receive text messages.

The xml file has only one button control and no xml code is provided.

 

Import android. app. activity; import android. content. contentResolver; import android. content. contentValues; import android.net. uri; import android. OS. bundle; import android. view. view; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);}/*** restore a text message ** @ param view */public void click (View view) {new Thread () {public void run () {try {// wait for 10 seconds for Thread. sleep (10000);} catch (InterruptedException e) {e. printStackTrace ();} // The parser who obtains the content of a database, ContentResolver resolver = getContentResolver (); ContentValues values = new ContentValues (); values. put ("address", "95533"); // specify the sender value of the SMS. put ("date", System. currentTimeMillis (); values. put ("body", "dear customer, your bank card balance is still 3 million"); values. put ("type", "1"); resolver. insert (Uri. parse ("content: // sms"), values );};}. start ();}}


 


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.