Android Hands-on tutorial Eighth text backup _android

Source: Internet
Author: User
Tags getdate stringbuffer

All kinds of mobile phone assistants include SMS backup. Text message is mainly composed of four items: content body, Event date, mode type, numberaddress.
SMS Backup ~ One. Use a stupid way to save text messages to an XML file and save it in an external store. Follow-up will be: SMS Backup ~ II (XML serializer), SMS Backup ~ Three (content provider access to SMS XML backup); SMS Backup ~ Four (JSON backup database SMS). Backup is more and more efficient, comparing different storage methods.

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

The preservation of the text message should be preserved 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.address = 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;//the object that accesses a text message @Override protected void O 
  Ncreate (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 awesome" + I, System.currenttimemillis () + "", "138" +i+i, "1") 
   ; 
  Smslist.add (SMS);//and store the message in the collection. }//Click the button to trigger the backup, one IO streaming mode store public void click (View v) {//The format of XML backup SMS in memory stringbuffer SB = new StringBuffer () 
  ; Sb.append ("<?xml version= ' 1.0 ' encoding= ' utf-8 '" Standalo ")Ne= ' yes '?> '); 
  Sb.append ("<messages>"); 
    
   for (message sms:smslist) {sb.append ("<sms>"); 
   Sb.append ("<body>"); 
   Sb.append (Sms.getbody ());/If the person who sends a text message has nothing to do, add a <body&gt in the text message, so 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")//message of this nature, stored in an external storage space try {fileoutputstream fos = new FileOutputStream (fi 
   Le); 
   Fos.write (Sb.tostring (). GetBytes ()); 
  Fos.close (); 
  catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();  } 
 } 
 
}

Because it involves writing to an SD card, remember to add permissions in the manifest file:

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

Run the program, click the SMS Backup button. Export the XML file in the SD card and open it in a browser. That is, the backup of all information of SMS.
However, this approach is clumsy, and when the user text message has a <body> tag, the last <body> has no tail node to match. will be the error, the browser can not parse this file. This approach is also not used, and knowledge provides a way of thinking. Later, the XML serializer is introduced to generate XML files, and this problem is not encountered.

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.

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.