Because the content of the text message has been exposed through contentprovider, so we can directly use content parser to obtain the text message content.
Some things you need to know to get the message content:
1, Uri uri = Uri.parse ("content://sms/");
2. Cursor cursor = getcontentresolver (). Query (uri,new string[]{"Address", "date", "Body"},null,null,null);
You know, it's a good idea to get information about a text message.
Let's look at the code first:
1 Public voidOnClick (View v) {2XmlSerializer serializer =Xml.newserializer ();3File File =NewFile (Environment.getexternalstoragedirectory (). GetPath (), "Smsbackup.xml");4 Try {5FileOutputStream OutputStream =Newfileoutputstream (file);6Serializer.setoutput (OutputStream, "Utf-8");7Serializer.startdocument ("Utf-8",true);8Serializer.starttag (NULL, "SMSs");9 TenUri uri = uri.parse ("content://sms/"); Onecursor cursor = getcontentresolver (). Query (URI,Newstring[]{"Address", "date", "Body"},NULL,NULL,NULL); A if(cursor!=NULL){ - while(Cursor.movetonext ()) { -String address = cursor.getstring (0); theString date = cursor.getstring (1); -String BODY = cursor.getstring (2); - - Try { + //SMS Node -Serializer.starttag (NULL, "SMS"); + //Address Node ASerializer.starttag (NULL, "Address"); at Serializer.text (address); -Serializer.endtag (NULL, "Address"); - //Date Node -Serializer.starttag (NULL, "Date"); - Serializer.text (date); -Serializer.endtag (NULL, "Date"); in //Body Node -Serializer.starttag (NULL, "Body"); to Serializer.text (body); +Serializer.endtag (NULL, "Body"); - theSerializer.endtag (NULL, "SMS"); * $}Catch(IOException e) {Panax Notoginseng e.printstacktrace (); - } theSystem.out.println (address+ "---" +date+ "---" +body); + } A } theSerializer.endtag (NULL, "SMSs"); + serializer.enddocument (); - outputstream.close (); $}Catch(FileNotFoundException e) { $ e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); the } - Wuyi}
It mainly uses content parser to get the information and content of SMS, then store it with XmlSerializer.
Here's manifest.xml: add these three permissions
1 < android:name= "Android.permission.READ_SMS"/>2 < android:name = "Android.permission.WRITE_SMS"/> 3 <android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" />
ContentProvider Backup SMS, stored as XML file