Sometimes the application needs to listen for changes in the ContentProvider and provide a response, this time using the Contentobserver class
Either the Insert,delete,update method is implemented in ContentProvider, and the program calls GetContext (). Getcontentresolver (). Notifychange (URI, NULL);
This line of code can be used to notify all listeners registered on the URI that the data shared by the ContentProvider has changed
Listeners listening for contentprovider data changes need to inherit the Contentobserver class and override the OnChange (Boolean Selfchange) method defined by the base class. When the data shared by the ContentProvider it listens to changes, the onchange will trigger
To listen for data changes for the specified ContentProvider, the Contentobserver listener needs to be registered with Contentresolver to the specified URI.
Use the following method to register the listener
Registercontentobserver (Uri Uri,boolean notifyfordescendents,contentobserver observer)
Notifyfordescendents: If the parameter is set to True, if the URI is content://abc, then the URI is content://abc/xyz, content://abc/xyz/ The listener is also triggered when the data of Foo is changed, and if the parameter is false, the listener will be triggered only if the CONTENT://ABC data changes
The following is an example of a listening system SMS, send a text message for the simulation
There is a message on the content of Hello, the following to start the listener in the LOGCAT print the message information
The following code
Package WangLi.IO.MonitorSms; 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 classMonitorsms extends Activity {/** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); //for content://SMS Data Change Registration listener This. Getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms"),true, NewSmsobserver (NewHandler ())); } //based on the custom Contentobserver listener class PrivateFinalclassSmsobserver extends Contentobserver { Publicsmsobserver (Handler Handler) {super (Handler); //TODO auto-generated Constructor stub } Public voidOnChange (Boolean selfchange) {//Query the sending box for text messages (messages in the sending State are placed in the Send box)cursor cursor = getcontentresolver (). Query (Uri.parse ("Content://sms/outbox"), NULL,NULL,NULL,NULL); //traverse the query to get the result set, you can get the text message that the user is sending while(Cursor.movetonext ()) {StringBuilder SB=NewStringBuilder (); //get the sending address of a text messageSb.append ("address="). Append (Cursor.getstring (Cursor.getcolumnindex ("Address"))); //get text message titleSb.append ("; subject="). Append (Cursor.getstring (Cursor.getcolumnindex ("Body"))); //Get SMS Send timeSb.append ("; Time="). Append (Cursor.getlong (Cursor.getcolumnindex ("Date"))); System. out. println ("Has Sent SMS: :"+sb.tostring ()); } } } }
Android, monitor ContentProvider data changes