I, preparatory work
The directory in which the system stores SMS content is:/dada/dada/com.android.providers.telephony/databases/mmssms.db, we find the corresponding database file.
We can find that the file is unreadable and unreadable to third parties and must be used by the content provider.
The problem is: I don't know the hostname, and I can't be a middleman to find out about the SFC. Actually, it doesn't matter at all, we can flip through the source code of Android:
\packages\providers\telephonyprovider the manifest file, because the hostname is configured on the manifest file, by flipping through, we find the information we need:
1 <provider android:name= "Smsprovider"2 android:authorities= "SMS"3 android: Multiprocess= "true"4 android:readpermission= "Android.permission.READ_SMS"5 android: writepermission= "Android.permission.WRITE_SMS"/>
The host name is "SMS", which also adds two permissions, so we also need to add read and write text messages to the project.
Finding the hostname is not enough, because we also need its matching rules, so it is easy to find, directly find the static code block in Smsprovider.java to see the matching rules:
1 Static {2 NULL , Sms_all); 3 Surlmatcher.adduri ("SMS", "#", sms_all_id); 4 ........................... 5 }
The first line means that when the hostname is NULL, it means that all SMS messages are matched.
In other words: content://sms means matching all SMS messages.
II. Get SMS Content
Set a button in the layout file, and in the Click event of the button, get the text message.
1 Public voidBackup (view view) {2 //Set Path3Uri uri=uri.parse ("Content://sms");4 /**5 * I want to realize the function is to read text messages, first get the middleman6 */7Contentresolver resolver=getcontentresolver ();8 /**9 * Projection represents the column of the queryTen */ OneCursor Cursor=resolver.query (URI,Newstring[]{"Address", "date", "type", "Body"},NULL,NULL,NULL); A}
Contentresolver resolver=getcontentresolver (); is to get the middleman.
The middleman then takes the address to query the SMS database, and the next step is to use the cursor to get the content of the text message.
1List<smsinfo> list=NewArraylist<smsinfo>();2 while(Cursor.movetonext ()) {3String address=cursor.getstring (0);4String date=cursor.getstring (1);5String type=cursor.getstring (2);6String body=cursor.getstring (3);7Smsinfo sms=NewSmsinfo (address, date, type, body);8 List.add (SMS);9 }TenSmsmanager.open (list, This);
Smsinfo is the JavaBean that uses it to put the data we want, the address of the SMS (sender), the date (the time of the transmission), the type (the text of the message), the body (the content of the text message) encapsulated in the bean.
Finally, it is stored in the list collection. Smsmanager is a tool class that can output the data in the collection as an XML file.
Smsmanager code is attached below:
1 Public Static voidOpen (list<smsinfo>List,context Context) {2 /**3 * Pull Parse output XML file4 */5 Try {6XmlSerializer serializer=Xml.newserializer ();7File file=NewFile (Environment.getexternalstoragedirectory (). GetPath () + "/backup.xml");8FileOutputStream fos=Newfileoutputstream (file);9 TenSerializer.setoutput (FOS, "Utf-8"); One ASerializer.startdocument ("Utf-8",true); -Serializer.starttag (NULL, "SMSs"); - for(Smsinfo sms:list) { theSerializer.starttag (NULL, "SMS"); - -Serializer.starttag (NULL, "Address"); - Serializer.text (Sms.getaddress ()); +Serializer.endtag (NULL, "Address"); - +Serializer.starttag (NULL, "Date"); A Serializer.text (Sms.getdate ()); atSerializer.endtag (NULL, "Date"); - -Serializer.starttag (NULL, "type"); - Serializer.text (Sms.gettype ()); -Serializer.endtag (NULL, "type"); - inSerializer.starttag (NULL, "Body"); - Serializer.text (Sms.getbody ()); toSerializer.endtag (NULL, "Body"); + -Serializer.endtag (NULL, "SMS"); the } *Serializer.endtag (NULL, "SMSs"); $ serializer.enddocument ();Panax Notoginseng fos.close (); -Toast.maketext (context, "Serializer mode output XML succeeded! ", 0). Show (); the}Catch(Exception e) { +Toast.maketext (Context, "throwing exception", 0). Show (); A e.printstacktrace (); the } +}
The content of the last text message is stored in an XML-formatted file.
The backup of the SMS is also completed.
"Android Essentials" Using content providers for SMS Backup