Android Four components Learning ContentProvider Five

Source: Internet
Author: User

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

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.