Android Development encountered SMS backup failed

Source: Internet
Author: User

Today did a small case of ContentProvider SMS Backup, encountered SMS backup failed, after a few setbacks and finally found the problem

The case is to write a text message to an XML file and then save in the phone storage to implement SMS Backup function, the key implementation code is as follows

 Public classSmsutils { Public Static voidBackupsms (list<smsinfo>Smsinfos, Context context) {        Try{XmlSerializer Serializer=Xml.newserializer (); File File=NewFile (Environment.getexternalstoragedirectory (), "Sms.xml"); //initializes the serializer, specifies which file the XML data is written to, and specifies how the file is encodedFileOutputStream OS =Newfileoutputstream (file); Serializer.setoutput (OS,"Utf-8"); Serializer.startdocument ("Utf-8",true); //Building the root nodeSerializer.starttag (NULL, "SMSs");  for(Smsinfo info:smsinfos) {//Build parent node start labelSerializer.starttag (NULL, "SMS"); Serializer.attribute (NULL, "id", Info.getid () + ""); //Building child nodes bodySerializer.starttag (NULL, "Body");                Serializer.text (Info.getbody ()); Serializer.endtag (NULL, "Body"); //Building a child node addressSerializer.starttag (NULL, "Address");                Serializer.text (Info.getaddress ()); Serializer.endtag (NULL, "Address"); //Building child nodes typeSerializer.starttag (NULL, "type"); Serializer.text (Info.gettype ()+ ""); Serializer.endtag (NULL, "type"); //Building a child node dateSerializer.starttag (NULL, "Date"); Serializer.text (Info.getdate ()+ ""); Serializer.endtag (NULL, "Date"); //parent node end tagSerializer.endtag (NULL, "SMS"); } Serializer.endtag (NULL, "SMSs");            Serializer.enddocument ();            Os.close (); Toast.maketext (Context,"Backup succeeded", Toast.length_long). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext (Context,"Backup Failed", Toast.length_long). Show (); }    }}

Here is the information printed by Logcat

07-13 16:44:18.195 26990-26990/wp.contentresolver d/stubcontroller:holdandgetpermissiontype permissionType:4 UID : 10382 pid:2699007-13 16:44:18.195 26990-26990/wp.contentresolver d/stubcontroller:addrequestcount, MRequestCount =1 mphoneidrequestcount:0 mlocationrequestcount:0 permissiontype is:407-13 16:44:18.195 26990-26990/wp.contentresolver D/stubcontroller:holdforgetpermissionselection mrequestcount:107-13 16:44:18.200 26990-26990/wp.contentresolver D/ STUBCONTROLLER:BEFORESHOWDIALOGCHECKRESULT:107-13 16:44:18.200 26990-26990/wp.contentresolver D/StubController: Minusrequestcount, Mrequestcount =0 mphoneidrequestcount:0 mlocationrequestcount:0 permissionType is:407-13 16:44:18.220 26990-26990/wp.contentresolver w/System.err:java.lang.IllegalArgumentException:Illegal character (d83d)07-13 16:44:18.220 26990-26990/wp.contentresolver W/system.err:at Org.kxml2.io.KXmlSerializer.reportInvalidCharacter (kxmlserializer.java:144)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at org.kxml2.io.KXmlSerializer.writeEscaped ( kxmlserializer.java:130)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at org.kxml2.io.KXmlSerializer.text ( kxmlserializer.java:536)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at wp.contentresolver.SmsUtils.backUpSms ( smsutils.java:56)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at Wp.contentresolver.MainActivity.click ( Mainactivity.java:36)07-13 16:44:18.220 26990-26990/wp.contentresolver w/System.err:at java.lang.reflect.Method.invokeNative (Native Method)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at java.lang.reflect.Method.invoke (method.java:515)07-13 16:44:18.220 26990-26990/wp.contentresolver W/system.err:at android.support.v7.app.appcompatviewinflater$ Declaredonclicklistener.onclick (appcompatviewinflater.java:288)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at Android.view.View.performClick (view.java:4446)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at android.view.view$performclick.run (View.java : 18480)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at android.os.Handler.handleCallback (Handler.java : 733)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at android.os.Handler.dispatchMessage (Handler.java : 95)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at android.os.Looper.loop (looper.java:136)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at android.app.ActivityThread.main ( activitythread.java:5315)07-13 16:44:18.220 26990-26990/wp.contentresolver w/System.err:at java.lang.reflect.Method.invokeNative (Native Method)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at java.lang.reflect.Method.invoke (method.java:515)07-13 16:44:18.220 26990-26990/wp.contentresolver W/system.err:at com.android.internal.os.zygoteinit$ Methodandargscaller.run (zygoteinit.java:864)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at com.android.internal.os.ZygoteInit.main ( zygoteinit.java:680)07-13 16:44:18.220 26990-26990/wp.contentresolver w/system.err:at dalvik.system.NativeStart.main (Native Method)

Because there is no obvious error message on the Logcat, the blind test on the real machine many times, is to fail, the code check many times there is no problem, so the feeling is the problem of mobile phone system, after all, there is such a situation before,

Take the classmate's mobile phone test, the results show that the backup was successful, guess is correct? --Of course not, look at the error message on the LOGCAT, continue to explore, asked the guide, began the wrong troubleshooting, the problem is ultimately positioned in the text to write to the XML,

First, the enhanced for loop is removed, only write a simple tail-end node, the result is successful, and then added a few sub-nodes, the text is written as fixed content, run up and back up successfully, so you can know that their real machine can write XML files normally

The content fixed by the Text property in the node is changed to read the content of the text message dynamically (Serializer.text (Smsinfos.get (0). GetBody ());) so that the real machine can read the text message content and write it to the XML.

The last is for the problem of the loop, because write a separate node can read normally, and read all the information will fail, so it can be explained that there is a problem in one of the information, a careful look at the information on the real machine, found inside there are expressions, so that all the information with the expression is deleted,

Again run a backup, the result is successful, the problem is finally solved, not the problem of the mobile phone system, the original is the conversion problem of the code, the expression in the text (except the band) can not be normal conversion utf-8 format characters, resulting in a backup failure, and later combined with the error log,

The above reported a Java.lang.IllegalArgumentException:Illegal character (d83d) information, meaning illegal parameter exception, the following also reported a fault at the body node, so you can draw Is the question of the content of the information

Therefore, you must carefully review the LOGCAT error message and troubleshoot each error, not to be confused by the surface phenomenon

Android Development encountered SMS backup failed

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.