ContentProvider Backup SMS, stored as XML file

Source: Internet
Author: User

Because the content of the text message has been exposed through contentprovider, so we can directly use content parser to obtain the text message content.

Some things you need to know to get the message content:

1, Uri uri = Uri.parse ("content://sms/");

2. Cursor cursor = getcontentresolver (). Query (uri,new string[]{"Address", "date", "Body"},null,null,null);

You know, it's a good idea to get information about a text message.

Let's look at the code first:

1  Public voidOnClick (View v) {2XmlSerializer serializer =Xml.newserializer ();3File File =NewFile (Environment.getexternalstoragedirectory (). GetPath (), "Smsbackup.xml");4         Try {5FileOutputStream OutputStream =Newfileoutputstream (file);6Serializer.setoutput (OutputStream, "Utf-8");7Serializer.startdocument ("Utf-8",true);8Serializer.starttag (NULL, "SMSs");9 TenUri uri = uri.parse ("content://sms/"); Onecursor cursor = getcontentresolver (). Query (URI,Newstring[]{"Address", "date", "Body"},NULL,NULL,NULL); A             if(cursor!=NULL){ -              while(Cursor.movetonext ()) { -String address = cursor.getstring (0); theString date = cursor.getstring (1); -String BODY = cursor.getstring (2); -  -                 Try { +                     //SMS Node -Serializer.starttag (NULL, "SMS"); +                     //Address Node ASerializer.starttag (NULL, "Address"); at Serializer.text (address); -Serializer.endtag (NULL, "Address"); -                     //Date Node -Serializer.starttag (NULL, "Date"); - Serializer.text (date); -Serializer.endtag (NULL, "Date"); in                     //Body Node -Serializer.starttag (NULL, "Body"); to Serializer.text (body); +Serializer.endtag (NULL, "Body"); -  theSerializer.endtag (NULL, "SMS"); *  $}Catch(IOException e) {Panax Notoginseng e.printstacktrace (); -                 } theSystem.out.println (address+ "---" +date+ "---" +body); +             } A         } theSerializer.endtag (NULL, "SMSs"); + serializer.enddocument (); - outputstream.close (); $}Catch(FileNotFoundException e) { $ e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); the         } - Wuyi}

It mainly uses content parser to get the information and content of SMS, then store it with XmlSerializer.

Here's manifest.xml: add these three permissions

1     <  android:name= "Android.permission.READ_SMS"/>2     <  android:name = "Android.permission.WRITE_SMS"/> 3      <android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" />

ContentProvider Backup SMS, stored as XML file

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.