In actual development, we sometimes need to store or back up more complex data. These data are characterized by many content, large structure, such as SMS Backup. We know that sharedpreferences and files (text files) can be very inefficient in storing such data. If you have learned Javaweb's friends, the first thing you may think of is a database. Of course the database is a solution, so is there any other solution? Today we're talking about how data is stored in Android development notes (i) other than sharedpreferences and files (text files), there are several ways to store data: XML files, SQLite data, and network.
1.3 Examples
3. xml:
Small case: below we have such a small case: is the short message backup. Let's first analyze the structure of a message (pictured below).
We see a text message including: Text message content, SMS send or accept time, offset number, type (1 to accept, 2 to send) four attributes (field). Try to use the previous sharedpreferences and files (text file) analysis How to backup? Since the data saved by Sharedpreferences is simply a form of key-value pairs, it is clearly impossible to store the structure of the message, which is more complex than the text. Files can be done to define a structural format to save, but it becomes cumbersome and inefficient when reading and writing.
XML Backup principle: The current mobile phone assistant SMS backup methods, although a variety of, but the XML format is still relatively classic one. All the text in a certain label format, write to the XML file, and then save it to the local. From the principle you can see I first of all is to generate XML files.
XML Backup SMS:
First, it describes the format in which it saves information: header files, root nodes (including start and end nodes), child nodes, and his attributes.
• Layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
tools:context= "${relativepackage}.${activityclass}" >
<button
android:layout_width= "Wrap_ Content "
android:layout_height=" wrap_content "
android:text=" Generate XML/>
</RelativeLayout>
Java code:
• How to get the message saved by the system, in order to show the introduction, there is no content provider here, I use for loop directly virtual a group of SMS. We know that the number of short messages in the phone is a few more than thousands, each message has four separate attributes, then we can give each message encapsulated into a JavaBean object, each object has four general properties.
Sms.java (JavaBean object)
Package com.bokeyuan.createxml.domain;
/** *
JavaBean * @author * *
/Public
class SMS {
private String address;
private String date;
Private String type;
Private String body;
Public String getaddress () {return address
;
}
public void setaddress (String address) {
this.address = address;
}
Public String GetDate () {return
date;
}
public void setdate (String date) {
this.date = date;
}
Public String GetType () {return
type;
}
public void SetType (String type) {
this.type = type;
}
Public String GetBody () {return body
;
}
public void Setbody (String body) {
this.body = body;
}
Public Sms (string address, string date, String type, string body) {
super ();
this.address = address;
This.date = date;
This.type = type;
This.body = body;
}
@Override public
String toString () {return
"Sms [address=] + address +", date= "+ Date +", type= "+ type
+ ", body=" + body + "]";
}
Mainactivity.java
Package com.bokeyuan.createxml;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
Import Com.bokeyuan.createxml.domain.Sms;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View; public class Mainactivity extends activity {private list<sms> smslist; @Override protected void OnCreate (Bundle sav Edinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); smslist = new
Arraylist<sms> (); Suppose 10 sms for (int i = 0; i < i++) {SMS SMS = new Sms ("+i+i", System.currenttimemillis () + "", "1", "Hello, comrade" +i
);
Smslist.add (SMS); } public void OnClick (View v) {//StringBuffer SB = new StringBuffer ();///Add attribute to SB sb.append ("<?xml version=\" 1.0\ "E
Ncoding=\ "utf-8\"?> ");
Sb.append ("<messages>");
for (Sms sms:smslist) {sb.append ("<message>"); Sb.append ("<address>"); Sb.append (Sms.getaddress ()); Sb.append ("≪/address> ");
Sb.append ("<date>");
Sb.append (Sms.getdate ());
Sb.append ("</date>");
Sb.append ("<type>");
Sb.append (Sms.gettype ());
Sb.append ("</type>");
Sb.append ("<body>");
Sb.append (Sms.getbody ());
Sb.append ("</body>");
Sb.append ("</message>");
} sb.append ("</messages>");
Write out of Store path file = new file ("Strorage/sdcard/sms.xml"); try {fileoutputstream fos = new FileOutputStream (file); Fos.write (Sb.tostring (). GetBytes ()); Fos.close ();} catch (Excep
tion e) {//TODO auto-generated catch block E.printstacktrace ();}} }
• Privileges: Android.permission.WRITE_EXTERNAL_STORAGE
The above to introduce the Android development notes on the Android data storage mode (ii) of the relevant knowledge, I hope you like.