Entity Bean for SMS
Package Com.examp.backsms.domain;public class Smsinfo {private string Address;private string date;private string type; private string Body;public Smsinfo () {}public smsinfo (string address, string date, String type, string body) {This.address = Address;this.date = Date;this.type = Type;this.body = Body;} 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 "Smsinfo [address=" + Address + ", date=" + Date + ", type=" + Type + ", body=" + Body + "]";}}
Core Code
Package Com.examp.backsms;import Java.util.arraylist;import Java.util.list;import android.app.activity;import Android.content.contentresolver;import Android.database.cursor;import Android.net.uri;import Android.os.Bundle; Import Android.view.view;import Com.examp.backsms.domain.smsinfo;import Com.examp.backsms.util.smsutils;public Class Mainactivity extends Activity { @Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (r.layout.activity_main); } public void Backsms (view view) { //create called host name uri Uri = Uri.parse ("content://sms/"); //Get content parser Contentresolver resolver = getcontentresolver (); //Execute query, get result set &Nbsp; cursor Cursor = Resolver.query (URI, new string[] {"Address", "date", &NB sp; "type", "body"}, NULL, NULL, NULL); Create a collection of SMS messages List<SmsInfo> infos = new arraylist<smsinfo> (); Add individually to collection while (Cursor.movetonext ()) { smsinfo info = new Smsinfo (); Info.setaddress (Cursor.getstring (Cursor.getcolumnindex ("Address")); info.setbody (cursor.getstring (Cursor.getcolumnindex ("Body")); info.setdate (cursor.getstring (Cursor.getcolumnindex ("date")); info.settype (cursor.getstring (Cursor.getcolumnindex ("type")); infos.add (info); }/ Class invoke Backup Business//Set Collection | The context of this activity incoming smsutils.backupsms (infos, this); & nbsp; cursor.close (); }}
Tool class
Package Com.examp.backsms.util;import Java.io.file;import Java.io.fileoutputstream;import java.util.List;import Org.xmlpull.v1.xmlserializer;import Android.content.context;import Android.os.environment;import android.util.Xml Import Android.widget.toast;import Com.examp.backsms.domain.smsinfo;public class Smsutils {public static void Backupsms (list<smsinfo> Smsinfos, context context) {try {//create XML serializer xmlserializer serializer = Xml.newserializer (); File File = new file (Environment.getexternalstoragedirectory (), "Backupsms.xml"); FileOutputStream OS = new FileOutputStream (file),//initializes the XML serializer, specifies that the XML data is written to that file, and formulates the encoding of the file Serializer.setoutput (OS, " Utf-8 ");//Set <?xml version=" 1.0 "encoding=" Utf-8 "?, and whether it is a standalone XML file Serializer.startdocument (" Utf-8 ", true);// Equivalent to <smss>serializer.starttag (null, "SMSS"); for (Smsinfo Info:smsinfos) {Serializer.starttag (null, "SMS"); Serializer.starttag (NULL, "body"); Serializer.text (Info.getbody ()); Serializer.endtag (null, "body"); Serializer.starttag (nulL, "type"); Serializer.text (Info.gettype () + ""); Serializer.endtag (null, "type"); Serializer.starttag (null, "address" ); Serializer.text (Info.getaddress ()); Serializer.endtag (null, "address"); Serializer.starttag (null, "date"); Serializer.text (Info.getdate () + ""), Serializer.endtag (null, "date"), Serializer.endtag (NULL, "SMS"); Serializer.endtag (NULL, "SMSS"); Serializer.enddocument (); Os.close (); Toast.maketext (Context, "Backup SMS successful, backup storage path:" + environment.getexternalstoragedirectory () + "Backsms.xml", toast.length_ Short). Show ();} catch (Exception e) {toast.maketext (context, "Backup SMS failed!!", Toast.length_short). Show (); E.printstacktrace ();}}
View Code, because it's just a case, there's only one button
<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:onclick=" backsms " android:text=" @string/bt_queryall "/></relativelayout>
Permission manifest file
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.examp.backsms "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <!--get permission to read and write text messages and <uses-permission Android:name= "Android.permission.READ_SMS"/> <uses-permission android:name= "Android.permission.WRITE_SMS"/ > <!--get permission to write to the SD card--<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@stri Ng/app_name "Android:theme=" @style/apptheme "> <activity android:name=" com.examp.backsms.Ma Inactivity "android:label=" @string/app_name "> <intent-filter> <action an Droid:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity& Gt </application></manifest>
Resource File String.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" >0019 SMS Backup </ string> <string name= "action_settings" >Settings</string> <string name= "Bt_queryall" > Backup SMS </string></resources>
Note: This code is only the case code, can not withstand too much data pressure, so the actual development time is the side-reading data side backup.