The last few sections have learned the practical uses of ContentProvider, reading text messages, inserting text messages, reading contacts, inserting contacts, and so on. This lesson is to learn the contentprovider of the viewer.
In life, there are third-party software, such as what SMS software, this SMS software is a substitute for the system's own SMS software. Of course, can read text messages is the use of contentprovider, but why such software can be in real-time to display the received text messages? This is used by our content watchers.
What is a content observer? is always observing whether the system has new text messages, if there is a new text message will prompt the third-party software quickly to the database to get text messages. So we can get the system's text messages in real time.
To illustrate:
public class Mycontentobserveractivity extends Activity { @Override protected void OnCreate (Bundle savedinstancestate) { //TODO auto-generated method stub super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_readcontact); /** * URI: listens to the content provider's notifications on that URI * true: "content://sms" is represented, content:// Sms/inbox "," Content://sms/outbox "can match * false:" content://sms "matches only this * The onchange method in this anonymous class is called when the data is changed */ getcontentresolver () registercontentobserver (Uri.parse ("Content://sms"), True, new contentobservER (new Handler ()) { @Override public void OnChange (Boolean selfchange) { //TODO auto-generated method stub & nbsp; //super.onchange (selfchange); log.i ("Mycontentobserveractivity", "New News!!!"); contentresolver Cresolver = Getcontentresolver (); /** &nBsp * URI: is the URL of the SMS Provider and can be obtained by viewing the source * Projection: Several fields to query, care about that query by the line * Sort by date, take last */ cursor cursor = cresolver.query (Uri.parse ("Content://sms"), New string[]{"Address", "date", "type", "Body"}, null, NULL, "date"); if (Cursor.movetolast ()) { 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); } } }); }}Then you can send a text message on the simulator and you'll see the console has a text message output
This will take you to the latest news.
It's here today.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four components Learning ContentProvider Five