Android Simple Combat Tutorial--eighth words "SMS Backup ~ a"

Source: Internet
Author: User

A variety of mobile phone assistants are included in the SMS backup this item. The main part of the text message contains four items: Content body, Event date, mode type, number address.

SMS Backup ~ One. Use a stupid way to save text messages to an XML file, and save it in an external store. The following will be: SMS Backup ~ two (XML serializer), SMS Backup ~ Three (content providers get SMS XML backup), SMS Backup ~ Four (JSON backup database SMS). Backups are increasingly efficient and compare different ways of storage.

Obviously, the initial introduction of SMS Backup, in a simple and clumsy way, and does not involve the content provider, virtual 10 SMS.

Save the text message in the form of an object. So create a JavaBean class:

Package Com.itydl.createxml.domain;public class Message {private string Body;private string Date;private string address; private string Type;public string GetBody () {return body;} public void Setbody (String body) {this.body = body;} Public String GetDate () {return date;} public void SetDate (String date) {this.date = date;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} Public String GetType () {return type;} public void SetType (String type) {this.type = type;} Public Message (string body, string date, string address, String type) {super (); this.body = Body;this.date = date;this.addr ESS = Address;this.type = type;}}
Then the code in the mainactivity:

Package Com.itydl.createxml;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.util.arraylist;import Java.util.list;import Com.itheima.createxml.domain.message;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;public class Mainactivity extends Activity {list<message> smsList;// An object that accesses a text message @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//virtual 10 SMS Smslist = new arraylist<message> (); for (int i = 0; i <; i++) { Message SMS = new Message ("Little chi Good" + I, System.currenttimemillis () + "", "138" +i+i, "1"); Smslist.add (SMS);//and store SMS in the collection. }}//Click the button to trigger the backup, an IO stream stores public void Click (View v) {/////In memory, the format of the XML backup SMS is StringBuffer SB = new StringBuffer (); Sb.append (" <?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?> '); Sb.append ("<messages>"); for (Message SMS: Smslist) {sb.append ("<sms>"); sB.append ("<body>"); Sb.append (Sms.getbody ());//If the person who sent the text message has nothing to do, add the <body&gt in the text message, and it will be an error. Sb.append ("</body>"); Sb.append ("<date>"); Sb.append (Sms.getdate ()); Sb.append ("</date>"); Sb.append ("<type>"); Sb.append (Sms.gettype ()); Sb.append ("</type>"); Sb.append ("<address>"); Sb.append (Sms.getaddress ()); Sb.append ("</address>"); Sb.append ("</sms>");} Sb.append ("</messages>"); File File = new file ("Sdcard/sms.xml");//The information of this nature of the text message, stored in the external storage space try {fileoutputstream fos = new FileOutputStream (file); Fos.write (Sb.tostring (). GetBytes ()); Fos.close (); catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Because it involves writing to the SD card, remember to include permissions in the manifest file:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Run the program and click the SMS Backup button. Export the XML file in the SD card and open it with the browser. That is, the backup of all information in SMS.

However, this approach is very clumsy, and when the user message message has a <body> tag, the last <body> there is no tail node to match. will be error, the browser cannot parse this file. This approach is not used, and knowledge provides a way of thinking. You will not encounter this problem if you follow the XML serializer to generate an XML file.

Android Simple Combat Tutorial--eighth words "SMS Backup ~ a"

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.