Learn how to create a contentprovider yourself in the last lesson. and use Contentresolver to operate the ContentProvider.
Today we use the ContentProvider provided by the system. First, a simple, read the system's SMS content.
According to the above section know. Since you want to read the system's text messages, then you have to find the system's SMS there?
First find the text message database problem, and then export, to see what's inside
Now that we have analyzed it, we need nothing. Of course, you need to know the URL of the SMS Provider to the third party.
This can be viewed through the Android source code.
Then we'll write a program that reads the SMS from the system.
First look at the code in the activity:
public class Readmessageactivity extends Activity {Button readmessage; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_readmessage) Readmessage = (Button) Findviewbyid (R.id.button1); Readmessage.setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (View arg0) {log.i ("readmessageactivity", "Read contact button pressed"); Contentresolver cresolver = Getcontentresolver ();/** * URI: is the URL of the SMS Provider. be able to get * projection by looking at the source code: several fields to query, care about that query by */cursor Cursor = Cresolver.query (Uri.parse ("Content://sms"), New String []{"Address", "date", "type", "body"}, NULL, NULL, NULL), while (Cursor.movetonext ()) {String address = cursor.getstring ( Cursor.getcolumnindex ("Address")); String date = cursor.getstring (Cursor.getcolumnindex ("date")); String type = cursor.getstring (Cursor.getcolumnindex ("type")); String BODY = cursor.getstring (Cursor.getcolumnindex ("body")); LOG.I ("readmessageactivity", Address + "; "+ Date +"; "+ type +"; "+ Body);}});}}
Layout file just a button, no explanation
Printing results are:
Ok. Read the system text message even if it is simply finished.
Then we write a text message to the system.
Let's add a button that inserts a text message. Then insert a text message when you press it:
The code is as follows:
Writemessage.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {// Write SMS button is pressed contentresolver cresolver = Getcontentresolver (); Contentvalues values = new Contentvalues (); Values.put ("Body", "sorry." Your phone number is 6789 because of tail number. Less than 1w yuan, has been shut down!Values.put ("Address", 10086), Values.put ("date", System.currenttimemillis ()), Values.put ("type", 1); Cresolver.insert (Uri.parse ("content://sms"), values);});
The results are as follows:
Can see the information inserted in the database already, the same time the dialog box appears. So inserting a text message is done.
Android Four components learning ContentProvider three