SoFunction
Updated on 2025-04-07

Android practical tutorial Chapter 9: Efficient backup of text messages

Efficient backup of this article by Android SMS,Previous article. Use an efficient way to back up text messages - the xml serializer.

Storing text messages should be stored in an object manner. First create a javabean:

package ; 
 
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) { 
   = body; 
 } 
 public String getDate() { 
  return date; 
 } 
 public void setDate(String date) { 
   = date; 
 } 
 public String getAddress() { 
  return address; 
 } 
 public void setAddress(String address) { 
   = address; 
 } 
 public String getType() { 
  return type; 
 } 
 public void setType(String type) { 
   = type; 
 } 
 public Message(String body, String date, String address, String type) { 
  super(); 
   = body; 
   = date; 
   = address; 
   = type; 
 } 
  
  
} 

Then the diamante in the mainactivity is as follows:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
 
import .; 
 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
//In order to better save different and responsible data, use the xml sequencerpublic class MainActivity extends Activity { 
 
 List<Message> smsList; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setContentView(.activity_main); 
  // 10 virtual text messages  smsList = new ArrayList<Message>(); 
  for (int i = 0; i < 10; i++) { 
   Message sms = new Message("Xiaozhi is great" + i, () 
     + "", "138" + i + i, "1"); 
   (sms); 
  } 
 } 
 
 public void click(View v){ 
  //Use the xml serializer to generate the xml file.  The serializer will filter out messy things (there are tag attributes in the text message)  //1. Get the serializer object  XmlSerializer xs = (); 
  //2. Initialization. Because it involves xml files, initialization also contains file name and path  File file = new File("sdcard/"); 
  try { 
   FileOutputStream fos = new FileOutputStream(file); 
   //encoding: Specify what encoding to use to generate the xml file   (fos, "utf-8");//setOutput(OutputStream os, String encoding) , the encoding method defaults to u8    
   //3. Start generating xml file   //enconding: Specify the value of the enconding attribute in the header node   ("utf-8", true);//Generate header node (highest line), startDocument(String encoding, Boolean standalone)    
   (null, "message");//Generate a root node and start tag node    
   for (Message sms : smsList) { 
    (null, "sms"); 
     
    (null, "body");//Writing start and end one-to-one.  There are four child nodes    (() + "&lt;body&gt;");//This way there will be no errors.  Instead, he treated <body> as content    (null, "body"); 
     
    (null, "date"); 
    (()); 
    (null, "date"); 
     
    (null, "type"); 
    (()); 
    (null, "type"); 
     
    (null, "address"); 
    (()); 
    (null, "address"); 
     
    (null, "sms"); 
   } 
    
   (null, "message"); 
    
   //Tell the serializer that the file generation has been completed   (); 
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   (); 
  } 
 } 
 
} 

Finally, remember to join permissions:

Copy the codeThe code is as follows:
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/> 

It can be seen that this method solves the problems encountered before and is much more efficient.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.