In real-world 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 and so on. We know that sharedpreferences and files (text files) can be very inefficient in storing such data. If you have learned Javaweb friends, the first thing you might think of is the database. Of course, the database is a solution, so is there another solution? Today we're talking about how the data is stored in the Android note--android (a) Several other data storage methods are mentioned except for sharedpreferences and files (text files) : XML files ,sqlite data , and Network.
1.3 Examples
3. xml:
Small case: below we have such a small case: is the SMS backup. Let's first analyze the structure of a text message (e.g.).
We see a text message including: SMS Content , the time the message was sent or received, the number of the other person , the type(1 is accepted, 2 is sent) four attributes (fields). Try to analyze how to back up with sharedpreferences and files (text files). Since the data stored by Sharedpreferences is simply a form of key-value pairs, it is clearly impossible for him to store it in relation to the structure of the text message. Files can do that by defining a structure format to save, but it becomes cumbersome and inefficient to read and write.
- XML Backup principle: At present, the mobile phone assistant SMS Backup mode although a variety of, but the XML format is still a more classical one. Put all the text messages in a certain label format, write to the XML file, and then save it to the local. The first thing I can see from its principle is to generate an XML file.
- XML Backup SMS:
It first describes the format in which it holds information: the header file, the root node (including the start node and end node), the child node, and his attributes.
- Layout file:
- Java code:
- How to get the text messages saved by the system, in order to show, there is no content provider, I here with For Loop directly virtual a group of SMS. We know that the number of short messages in the cell phone is a few and thousands, each text message has four independent attributes, then we can give each text message encapsulated into a JavaBean object, each object has four general properties.
- Sms.java (JavaBean object)
PackageCom.bokeyuan.createxml.domain;/*** JavaBean of SMS content properties *@author * */ Public classSms {PrivateString address; PrivateString date; PrivateString type; PrivateString body; PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; } PublicString getDate () {returndate; } Public voidsetDate (String date) { This. Date =date; } PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; } PublicString GetBody () {returnbody; } Public voidSetbody (String body) { This. BODY =body; } PublicSms (string address, string date, String type, string body) {Super(); This. Address =address; This. Date =date; This. Type =type; This. BODY =body; } @Override PublicString toString () {return"Sms [address= + address +", date= "+ Date +", type= "+type+ ", body=" + body + "]"; } }
- Mainactivity.java
PackageCom.bokeyuan.createxml;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportCom.bokeyuan.createxml.domain.Sms;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classMainactivityextendsActivity {PrivateList<sms>smslist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Smslist=NewArraylist<sms>(); //Suppose 10 messages for(inti = 0; I < 10; i++) {SMS SMS=NewSms ("+i+i", System.currenttimemillis () + "" "," 1 "," Hello, comrade "+i); Smslist.add (SMS); } } Public voidOnClick (View v) {//StringBuffer SB =NewStringBuffer (); //adding attributes to SBSb.append ("<?xml version=\" 1.0\ "encoding=\" 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 to an out of store pathFile File =NewFile ("Strorage/sdcard/sms.xml"); Try{FileOutputStream fos=Newfileoutputstream (file); Fos.write (Sb.tostring (). GetBytes ()); Fos.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
- Permissions: Android.permission.WRITE_EXTERNAL_STORAGE
Resources:
Data storage and interface presentation based on Android application development (III.)
How to store data in Android note--android (ii)