Android developer _ backup SMS

Source: Internet
Author: User

Android developer _ backup SMS
Zookeeper

Principles of text message backup

The principle of text message backup is to use the content provider to read and save the text message.

Public class SmsBackupUtils {// callback interface public interface SmsBackupCallback {/*** total number of messages called * @ param total text messages */public void beforeSmsbackup (int total) before SMS backup ); /*** call * @ param progress SMS progress */public void progressSmsbackup (int progress);}/*** to back up the SMS, back up the file to an xml file because the xml file is highly cross-platform. This is a time-consuming operation, it should be placed in the Child thread to execute ** @ param context Context * @ param path which path to back up * @ throws Exception */public static void smsBackup (context, String path, SmsBackupCallback callBack) throws Exception {// xml serializer XmlSerializer serializer = Xml. newSerializer (); File file = new File (path); FileOutputStream fos = new FileOutputStream (file); // you can specify the serializer parameter. setOutput (fos, "UTF-8"); // start serializer. startDocument ("UTF-8", true); serializer. startTag (null, "smss"); ContentResolver resolver = context. getContentResolver (); Uri uri = Uri. parse ("content: // sms"); // contains all sms Cursor cursor = resolver. query (uri, new String [] {"address", "date", "type", "body"}, null);/* dialog. setMax (cursor. getCount (); progressBar1.setMax (cursor. getCount (); */callBack. beforeSmsbackup (cursor. getCount (); int progress = 0; while (cursor. moveToNext () {serializer. startTag (null, "sms"); serializer. startTag (null, "address"); String address = cursor. getString (0); serializer. text (address); serializer. endTag (null, "address"); serializer. startTag (null, "date"); String date = cursor. getString (1); serializer. text (date); serializer. endTag (null, "date"); serializer. startTag (null, "type"); String type = cursor. getString (2); serializer. text (type); serializer. endTag (null, "type"); serializer. startTag (null, "body"); String body = cursor. getString (3); serializer. text (body); serializer. endTag (null, "body"); serializer. endTag (null, "sms"); progress ++;/* dialog. setProgress (progress); progressBar1.setProgress (progress); */callBack. progressSmsbackup (progress); SystemClock. sleep (1000);} cursor. close (); serializer. endTag (null, "smss"); serializer. endDocument ();}}

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.