Backup SMS Restore SMS in Android

Source: Internet
Author: User

Backup SMS

Read the SMS from your phone and save it back to an SD card.

The XML file has only one button control, and the XML code is no longer given.

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;/** * SMS Backup File object */private files 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.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {try {// Create a file object that stores backup text messages Smsbackupfile = new file (Environment.getexternalstoragedirectory (), "SMS.xml ");//Create a generator for an XML file. XmlSerializer serializer = Xml.newserializer (); Completes the serializer initialization operation. FileOutputStream OS = new FileOutputStream (smsbackupfile); Serializer.setoutput (OS, "Utf-8");//content provider. Gets the parser for the contents of a database Contentresolver resolver = Getcontentresolver ();//cursor (result set) cursor cursor = Resolver.query (Uri.parse (" Content://sms "), New string[]{" Address "," date "," type "," body "}, NULL, null,null);// Generates the header of the XML file 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 (addr); 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 Complete", 1). Show ();    catch (Exception e) {e.printstacktrace ();}}}); }   }

Restore SMS

Via code control to send SMS to mobile phone, 10s phone can receive SMS

The XML file has only one button control, and the XML code is no longer given.

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 10 seconds thread.sl EEP (10000);} catch (Interruptedexception e) {e.printstacktrace ();} Gets the parser for the contents of a database Contentresolver resolver = Getcontentresolver (); Contentvalues values = new Contentvalues () values.put ("Address", "95533");//Specifies the sender of the text message values.put ("date", System.currenttimemillis ()); Values.put ("Body", "Dear customer, your card balance is still 3 million"), Values.put ("type", "1"); Resolver.insert ( Uri.parse ("Content://sms"), values);};}. Start ();}}



Backup SMS in Android restore SMS

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.