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= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " 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 Java.io.fileoutputstream;import Java.io.ioexception;import java.util.ArrayList; Import Java.util.list;import Org.xmlpull.v1.xmlserializer;import Com.wzw.backupsms.entity.smsinfo;import Android.net.uri;import Android.os.bundle;import Android.app.activity;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.