The purpose of the contentobserver--Content Viewer is to observe (capture) the changes in the database caused by a particular URI, and then do some corresponding processing, similar to the
A Trigger in database technology (Trigger) that fires 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, which is related to the URI MIME type it listens to.
Familiarity with content Provider should be aware 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 results returned by the URI, the URI type can be divided into URIs that return multiple data, and URIs that return a single piece of data.
Register/Unregister The Contentobserver method, the abstract class Contentresolver the method prototype in the class as follows:
Public final void Registercontentobserver (Uri uri, Boolean notifyfordescendents, Contentobserver observer)
Function: Registers a contentobserver derived class instance for the specified URI, and recalls the instance object when the given URI has changed.
Parameters: Uri, Uri to be observed (need to be registered in Urimatcher, otherwise the URI has no meaning)
Notifyfordescendents is false to exact match, that is, only the URI is matched
True indicates that the URI of its derivation can be matched at the same time as the following example:
Suppose the URI registered in the 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 (pupil, derived URI)
Suppose that the URI we are currently looking for is content://com.qin.cb/student, if the URI of the data change is
Content://com.qin.cb/student/schoolchild, when Notifyfordescendents is false, then the contentobserver will not be monitored,
However, when Notifyfordescendents is ture, the database change of the URI can be captured.
Observer Contentobserver instance of a derived class
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 contentobserver derived classes need to call the constructor method
Parameter: Handler handler object. Can be the main thread handler (you can update the UI at this time) or any handler object.
Common methods
void OnChange (Boolean selfchange)
Function: When the observed URI changes, the callback method is processed. All contentobserver derived classes need to overload the method to handle the logic.
Parameters: Selfchange callback, the value is generally false, the parameter is not meaningful (I do not understand, the most important way to understand).
The other two methods, not very useful, I do not understand, we refer to the SDK to understand their own, presumptuous.
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 specific URI are as follows:
1, create our specific contentobserver derived class, must overload the parent class construction method, must overload onchange () method to deal with the callback function implementation
2, using Context.getcontentresolover () to obtain the Contentresolove object, and then invoke the Registercontentobserver () method to register the content observer
3, because the life cycle of contentobserver is different from the activity and service, so, when not needed, the need for manual call
Unregistercontentobserver () to cancel the registration.
Well, the basic explanation is here. Here is a brief description of the small demo:
The demo has two different contentobserver derived classes, as follows:
1, to observe whether the system changed the state of flight mode,
PS: You can go to the SDK to view the class: Android.provider.Settings.System. This class encapsulates access to all values under the settings module, such as:
Flight mode status, Bluetooth status, screen brightness values, and so on, and provide the corresponding URI.
2, observing the system's short message data has changed. When the monitor hears the message data change, inquires all sent text messages and displays them.
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 to send a list (for example, after the flight mode, the text message is in the outgoing column)
Use examples:
Package com.example.android_contentobserver;
Import android.app.Activity;
Import Android.database.ContentObserver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.os.Handler; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Supe
R.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); Registered observer Observser This.getcontentresolver (). Registercontentobserver (Uri.parse ("Content://sms"), true,new
Smsobserver (New Handler ()); Private Final class Smsobserver extends Contentobserver {public smsobserver (Handler Handler) {super (ha
Ndler); @Override public void OnChange (Boolean selfchange) {Cursor Cursor = MainActivity.this.getContentR
Esolver (). Query (Uri.parse ("Content://sms/inbox"), NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {StringBuilder sb = new StringbuilDer ();
Sb.append ("address="). Append (Cursor.getstring (Cursor.getcolumnindex ("Address"));
Sb.append ("; subject="). Append (Cursor.getstring (Cursor.getcolumnindex ("subject"));
Sb.append ("; body="). Append (Cursor.getstring (Cursor.getcolumnindex ("body"));
Sb.append ("; Time="). Append (Cursor.getlong (Cursor.getcolumnindex ("date"));
System.out.println ("--------has receivered SMS::" + sb.tostring ());
}
}
}
}