Android Hands-on tutorial nineth text message efficient backup _android

Source: Internet
Author: User
Tags getdate

Android SMS efficiently backs up this article and takes on an article. Use efficient ways to back up SMS--xml serializers.

Store text messages and store them in an object way. First create JavaBean:

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.address = address; 
  This.type = type; 
 } 
  
  

The

  then diamante in Mainactivity is as follows:

Package Com.itydl.xmlserializer; 
Import Java.io.File; 
Import Java.io.FileOutputStream; 
Import java.io.IOException; 
Import java.util.ArrayList; 
 
Import java.util.List; 
 
Import Org.xmlpull.v1.XmlSerializer; 
 
Import Com.itheima.createxml.domain.Message; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import android.util.Xml; 
Import Android.view.Menu; 
Import Android.view.View; 
 
 To better preserve the different and responsible data, use the XML serializer public class Mainactivity the extends activity {list<message> smslist; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); 
  Virtual 10 Message Smslist = new arraylist<message> ();  for (int i = 0; i < i++) {Message SMS = new Message ("Small zhi good" + I, System.currenttimemillis () + "", "138" + 
   i + I, "1"); 
  Smslist.add (SMS); } public void Click (View v) {//XML file is generated using the XML serializer. The serializer filters out the//1 by filtering out the clutter (the tag attribute in the SMS). Get the Serializer object xmlserializeR xs = Xml.newserializer (); 
  2. Initialization. Because the XML file is involved, initialization also contains the filename and path file = new file ("Sdcard/sms2.xml"); 
   try {fileoutputstream fos = new FileOutputStream (file); 
    
   Enconding: Specifies what encoding is used to generate the XML file Xs.setoutput (FOS, "Utf-8"),//setoutput (OutputStream OS, String encoding), encoding by default U8  3. Start generating XML file//enconding: Specifies the value of the Enconding property in the header node xs.startdocument ("Utf-8", true);//Generation header node (highest row), Startdocument (String  Encoding, Boolean Standalone) Xs.starttag (null, "message");//Generate a root node, start tag node for (message sms:smslist) 
     
    {Xs.starttag (NULL, "SMS"); Xs.starttag (NULL, "body");//start and end a pair of write. There are also four sub nodes Xs.text (sms.getbody () + "<body>");//This will not be an error. 
     
    But the <body> as the content of the Xs.endtag (null, "body"); 
    Xs.starttag (NULL, "date"); 
    Xs.text (Sms.getdate ()); 
     
    Xs.endtag (NULL, "date"); 
    Xs.starttag (NULL, "type"); 
    Xs.text (Sms.gettype ()); 
     
    Xs.endtag (NULL, "type"); 
    Xs.starttag (NULL, "address"); Xs.text (sms.geTaddress ()); 
     
    Xs.endtag (NULL, "address"); 
   Xs.endtag (NULL, "SMS"); 
    
   } Xs.endtag (NULL, "message"); 
  Tells the serializer that the file was generated xs.enddocument (); 
  catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();  } 
 } 
 
}

Finally, remember to add the right:

Copy Code code as follows:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

See this way to solve the problems encountered before, but also a lot of efficiency.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.