Android Storage Learning Save system SMS to SD card

Source: Internet
Author: User

This section learns to save the system SMS to the SD card. Since is to save the system SMS to the SD card, the premise is to get the system of SMS, about how to read the system text messages, please see my about ContentProvider article:Android Four Components learning ContentProvider three


Now that we know the purpose of the task, we will realize it directly. We will first read the system's SMS, then save to the XML file, and then write the XML file to the SD card.

1: First read the system SMS

Get contentresolvercontentresolver CR = Getcontentresolver ();//query 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);}

2: Where message is the class I have defined about SMS

public class Message {private string address;//number private string date;    Time private String type;    Type: 1 for the occurrence, 2 for the acceptance of the private String body;    SMS Content 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;}  @Overridepublic String toString () {return "Message [address=" + Address + ", date=" + Date + ", type=" + Type + ", body=" + Body + "]";} Public Message (string address, string date, String type, string body) {super (); this.address = Address;this.date = Date;thi S.type = Type;this.body = Body;}}

3: Stitching the contents of a text message into an XML file

Use StringBuffer to splice text messages into XML files    stringbuffer sbuffer = new StringBuffer ();    The header of the XML file    sbuffer.append ("<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>");    XML file start tag    sbuffer.append ("<Message>");    for (Message sms:list) {sbuffer.append ("<sms>");//Insert Contact Number field sbuffer.append ("<address>"); sbuffer.append (Sms.getaddress ()); Sbuffer.append ("</address>");//Insert Contact Type field sbuffer.append ("<type>"); sbuffer.append (Sms.gettype ()); Sbuffer.append ("</type>");//Insert the contents of the message sbuffer.append ("<body>"); Sbuffer.append ( Sms.getbody ()); Sbuffer.append ("</body>");//The Time Sbuffer.append ("<date>") in which the message was inserted; Sbuffer.append ( Sms.getdate ()); Sbuffer.append ("</date>"); Sbuffer.append ("</sms>");}    End tag    sbuffer.append ("</Message>");

4: Write the XML file to the SD card

File File = new file ("Sdcard/sms.xml");    try {fileoutputstream fos = new FileOutputStream (file); Fos.write (Sbuffer.tostring (). GetBytes ()); Fos.close ();} catch ( Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}

The entire code flow is:

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); }//UseStringBuffer the text message content into an XML file StringBuffer sbuffer = new StringBuffer ();    The header of the XML file Sbuffer.append ("<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>");    XML file start tag sbuffer.append ("<Message>"); for (Message sms:list) {sbuffer.append ("<sms>");//Insert Contact Number field sbuffer.append ("<address>"); sbuffer.append (Sms.getaddress ()); Sbuffer.append ("</address>");//Insert Contact Type field sbuffer.append ("<type>"); sbuffer.append (Sms.gettype ()); Sbuffer.append ("</type>");//Insert the contents of the message sbuffer.append ("<body>"); Sbuffer.append ( Sms.getbody ()); Sbuffer.append ("</body>");//The Time Sbuffer.append ("<date>") in which the message was inserted; Sbuffer.append (    Sms.getdate ()); Sbuffer.append ("</date>"); Sbuffer.append ("</sms>");}        End tag Sbuffer.append ("</Message>");    File File = new file ("Sdcard/sms.xml"); try {fileoutputstream fos = new FileOutputStream (file); Fos.write (Sbuffer.tostring (). GetBytes ()); Fos.close ();} catch ( Exception e) {//TODO auto-generated catch blocKe.printstacktrace ();} }}
When you click the button to save the system SMS, you can find the Sms.xml file in the SD card directory.

Export XML File Open:


Take one of these messages as an example:

Ok. about how to back up text messages here



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

Android Storage Learning Save system SMS to SD card

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.