Use of Contentobserver

Source: Internet
Author: User

contentobserver--content Observer, the purpose is to observe (capture) a particular URI caused by the changes in the database, and then do some corresponding processing, it is similar to

Trigger (Trigger) in database technology, which is triggered when the URI observed by contentobserver changes. Triggers are divided into table triggers, row triggers,

Accordingly, Contentobserver is also divided into "table" Contentobserver, "line" contentobserver, of course, which is related to the URI MIME type it listens to.



Familiarity with content Provider should know that we can register different types of URIs through the Urimatcher class, and we can use these different

URI to query for different results. Depending on the result returned by the URI, the URI type can be categorized as a URI that returns multiple data, a URI that returns a single piece of data.





To register/unregister the Contentobserver method, the method prototype in the abstract class Contentresolver class is as follows:



Public final void Registercontentobserver (Uri uri, Boolean notifyfordescendents, Contentobserver observer)

Function: Registers a contentobserver derived class instance for the specified URI and, when the given URI changes, callbacks the instance object to be processed.

Parameters: URI required to observe the URI (need to register in Urimatcher, otherwise the URI is meaningless)

Notifyfordescendents is false to indicate an exact match, that is, match only the URI

True indicates that it can match its derived URI at the same time, as in the following example:

Suppose the URI registered in Urimatcher has a common type:

1. Content://com.qin.cb/student (Student)

2, content://com.qin.cb/student/#

3, Content://com.qin.cb/student/schoolchild (elementary school, derived URI)



Suppose we currently need to observe the URI for content://com.qin.cb/student, if data changes occur with the URI

Content://com.qin.cb/student/schoolchild, when Notifyfordescendents is false, then the contentobserver will not listen,

But when Notifyfordescendents is ture, it can catch the database change of the URI.



Example of a derived class of observer contentobserver





Public final void Unregistercontentobserver (Contentobserver observer)

Function: Cancels the observation of a given URI

Parameters: Observer Contentobserver instances of derived classes





Contentobserver Class Introduction



Construction method public void Contentobserver (Handler Handler)

Description: All derived classes of contentobserver need to call the constructor method

Parameters: Handler Handler object. It can be the main thread handler (which can update the UI at this time), or it can be any handler object.

Common methods

void OnChange (Boolean selfchange)

Function: When the observed URI changes, callback the method to process. All derived classes of contentobserver need to overload the method to handle the logic.

Parameters: Selfchange callback, the value is generally false, the parameter is not very meaningful (I do not understand, the most important way to understand).



The other two methods, the use of small, I do not understand, we refer to the SDK to understand, take the liberty.

Boolean deliverselfnotifications ()

Description: Returns True if this observer was interested in notifications for changes made through the cursor The Observer is Regist Ered with.



Final void Dispatchchange (Boolean selfchange)





The steps to observe a particular URI are as follows:



1. To create our specific Contentobserver derived class, you must overload the parent class construction method, and you must overload the OnChange () method to handle the function implementation after the callback

2, use Context.getcontentresolover () to obtain the Contentresolove object, and then call the Registercontentobserver () method to register the content observer

3, because the life cycle of contentobserver is different from activity and service, so it needs to be called manually when it is not needed.

Unregistercontentobserver () to cancel the registration.







Well, the basic explanation is here. Here is a brief description of the demo:

The demo has two different contentobserver derived classes, as follows:

1, to observe whether the system changes the flight mode state,

PS: You can go to the SDK to see the class: Android.provider.Settings.System. This class encapsulates the access to all the values under the Setup module, such as:

Flight mode status, Bluetooth status, screen brightness value, etc., and provide the appropriate URI.

2, the observation system of short message data has changed. When the monitor hears the text message data changes, queries all sent SMS and displays.



The URI of the text message has a few:

Content://sms/inbox Inbox
Content://sms/sent has been sent
Content://sms/draft Draft
Content://sms/outbox Outbox (information being sent)
content://sms/failed Send failed
Content://sms/queued the list to be sent (for example, when airplane mode is on, the text message is in the outgoing column)

Use of Contentobserver

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.