Android Development SMS Backup Small Example

Source: Internet
Author: User

The main use of content providers ContentProvider

#1. Add write SDcard permissions to the Activity_main.xml layout file and add the right to read text messages

<relativelayout xmlns:android= ""    xmlns:tools= "http// "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <button        android:layout_width=" wrap_content "        android:layout_height=" Wrap_ Content "        android:layout_margintop=" 50DP "        android:layout_centerhorizontal=" true "        Android:onclick = "Backupsms"        android:text= "Backup SMS"/></relativelayout>

#2. The contents of the mainactivity are as follows

Package Com.wzw.backupsms;import;import;import java.util.ArrayList; Import Java.util.list;import Org.xmlpull.v1.xmlserializer;import Com.wzw.backupsms.entity.smsinfo;import;import Android.os.bundle;import;import Android.content.ContentResolver; Import Android.database.cursor;import android.util.log;import Android.util.xml;import Android.view.Menu;import Android.view.view;import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Backupsms (View v) {Uri uri=uri.parse ("content://sms/"); Contentresolver resolver = Getcontentresolver (); cursor cursor = resolver.query (URI, new string[]{"_id", "Address", "date", "type", "body"}, NULL, NULL, NULL); if (cursor!= Null&&cursor.getcount () >0) {list<smsinfo> smslist=new arraylist<smsinfo> (); Smsinfo SMSinfo;while (Cursor.movetonext ()) {//control cursor pointer, move down one smsinfo=new smsinfo (); Smsinfo.setid (Cursor.getint (0));// Set Idsmsinfo.setaddress (cursor.getstring (1));//Set SMS number Smsinfo.setdate (Cursor.getlong (2));//Set SMS Time Smsinfo.settype (Cursor.getint (3));//Set SMS Type Smsinfo.setbody (cursor.getstring (4));//Set SMS Content Smslist.add (smsinfo);} Cursor.close (); Writetolocal (smslist);}} /** * Serialized to local */private void writetolocal (list<smsinfo> smslist) {XmlSerializer serializer=xml.newserializer (); try {fileoutputstream fos=new fileoutputstream ("/mnt/sdcard/sms.xml"); Serializer.setoutput (FOS, "utf-8"); Serializer.startdocument ("Utf-8", true); Serializer.starttag (null, "SMSS"); for (Smsinfo smsinfo:smslist) { Serializer.starttag (NULL, "SMS"); Serializer.attribute (NULL, "id", string.valueof (Smsinfo.getid ()));// Write address Serializer.starttag (NULL, "address"), Serializer.text (Smsinfo.getaddress ()); Serializer.endtag (null, "address") ;//write type Serializer.starttag (null, "type"); Serializer.text (String.valueof (Smsinfo.gettype ())); Serializer.endtag ( Null"type");//write Time Serializer.starttag (null, "date"); Serializer.text (String.valueof (Smsinfo.getdate ())); Serializer.endtag (NULL, "date");//write content Serializer.starttag (null, "body"); Serializer.text (Smsinfo.getbody ()); Serializer.endtag (NULL, "body"); Serializer.endtag (null, "SMS");} Serializer.endtag (NULL, "SMSS"); Serializer.enddocument (); Toast.maketext (This, "Congratulations, Backup succeeded!")  ", 0). Show ();} catch (Exception e) {Toast.maketext (this, "I go, Backup failed! ", 0). Show (); E.printstacktrace ();}}

#3. The result of the write

<smss><sms id= "3" ><address>10086</address><type>1</type><date> 1406446124317</date><body>just for Test!</body></sms><sms id= "2" ><address>110 </address><type>1</type><date>1406446024971</date><body>world</body> </sms><sms id= "1" ><address>5556</address><type>1</type><date> 1406446006018</date><body>hello</body></sms></smss>

Record the Android development point drip.

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: 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.