Example of text message backup for Android Development

Source: Internet
Author: User

Example of text message backup for Android Development

It mainly uses content provider ContentProvider

 

#1. Add the write sdcard permission to the activity_main.xml layout file and the read SMS permission.

 

 
  
 

#2. MainActivity content:

 

 

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. X Ml; 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. qu Ery (uri, new String [] {_ id, address, date, type, body}, null); if (cursor! = Null & cursor. getCount ()> 0) {List
 
  
SmsList = new ArrayList
  
   
(); SmsInfo smsInfo; while (cursor. moveToNext () {// control the cursor, move down one smsInfo = new SmsInfo (); smsInfo. setId (cursor. getInt (0); // sets IDsmsInfo. setAddress (cursor. getString (1); // set the SMS number smsInfo. setDate (cursor. getLong (2); // set the SMS time to smsInfo. setType (cursor. getInt (3); // set the SMS type smsInfo. setBody (cursor. getString (4); // sets smsList for the text message. add (smsInfo);} cursor. close (); WriteToLocal (smsList) ;}/ *** serialize to local */private void WriteToLocal (List
   
    
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); serialize R. 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, the backup is successful !, 0). show ();} catch (Exception e) {Toast. makeText (this, I am going, backup failed !, 0). show (); e. printStackTrace ();}}}
   
  
 

#3. Write results

 

 

 
  
   10086
   
    1
   
   
    1406446124317
   just for test!
  
  
   110
   
    1
   
   
    1406446024971
   world
  
  
   5556
   
    1
   
   
    1406446006018
   hello
  
 


 

 

 

 

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