Android Storage Learning Save system SMS to SD card (using XML serializer)

Source: Internet
Author: User

We were stitching the XML file manually in the previous section, but there was a problem with that in the previous section, such as:

Insert the contents of the message sbuffer.append ("<body>"), Sbuffer.append (Sms.getbody () + "</body>"), Sbuffer.append ("</ Body> ");

I added a </body> in the Append content when I inserted the message content. This will cause the parsing error to occur.

So in this section we use the XML serializer to stitch up the text message content.


The following code is how to stitch a file with an XML serializer:

public class Mainactivity extends Activity {list<message> List;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);    List = new arraylist<message> ();        public void Storagemessage (View v) {//get contentresolver contentresolver cr = Getcontentresolver ();  Query the system for SMS, only need to query the fields we care about cursor cursor = cr.query (Uri.parse ("Content://sms"), New string[]{"Address", "date", "type",        "Body"}, NULL, NULL, NULL);    Remove the queried information while (Cursor.movetonext ()) {String address = cursor.getstring (Cursor.getcolumnindex ("Address"));    String date = cursor.getstring (Cursor.getcolumnindex ("date"));    String type = cursor.getstring (Cursor.getcolumnindex ("type"));        String BODY = cursor.getstring (Cursor.getcolumnindex ("body"));    Put each text message in the list collection message msg = new Message (address, date, type, body);    List.add (msg); }//Take FirstTo the XML serialization object XmlSerializer xs = Xml.newserializer ();    File File = new file ("Sdcard/sms2.xml"); try {fileoutputstream fos = new FileOutputStream (file),//utf-8 used to specify what encoding to generate the XML file Xs.setoutput (FOS, "utf-8");// UTF-8 Specifies the value of the Enconding attribute in the header node xs.startdocument ("Utf-8", true); Xs.starttag (NULL, "message"); for (message sms:list) { Xs.starttag (NULL, "SMS"), Xs.starttag (NULL, "address"), Xs.text (Sms.getaddress ()); Xs.endtag (null, "address"); Xs.starttag (NULL, "type"), Xs.text (Sms.gettype ()), Xs.endtag (null, "type"), Xs.starttag (null, "date"), Xs.text ( Sms.getdate ()); Xs.endtag (null, "date"), Xs.starttag (null, "body"), Xs.text (Sms.getbody () + "<body>"); Xs.endtag (NULL, "body"); Xs.endtag (null, "SMS");} Xs.endtag (NULL, "message");//tells the serializer that the file has been generated xs.enddocument ();}        catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}   }}

As you can see, I intentionally added a body to the content of the text message.

Export to query the backup file information:

You can see that the XML serializer has been automatically processed internally.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Storage Learning Save system SMS to SD card (using XML serializer)

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.