Intercept text messages, such as when texting, text messages read out, when the system's text message changes, shouting, the data sent to the public message mailbox, our application through the content observers observe the public message mailbox
Gets the Contentresolver object, calls the function Getcontentresolver (),
Call the Registercontentobserver (Uri,notifyfordescendents,observer) method of the Contentresolver object, Parameter: Uri object, whether the exact URI (true is imprecise, FALSE), observer object Contentobserver object
Because Contentobserver is an abstract class, so we write an inner class to inherit this abstract class, the constructor must be implemented, the handler object message handler of the constructor will speak later, define an inner class myobserver, implement the onchange () of the parent class callback method, which is observed when the message mailbox is changed.
In this callback function, the Movetofirst () pointer to the cursor object is used to retrieve the text message and take the last one, pointing to the last bar
How does the system application make this call, get the Contentresolver object, and, through the Getcontentresolver () method, invoke the Notifychange (Uri,observer) method of the Contentresolver object, Parameters: URI arbitrarily defined, observer specifies who handles default null
Many of the system's applications for notification communication are implemented through this public message mailbox mechanism.
Next, the project is based on the projects used in the Android content provider implementation, creating a new project and adding code as follows:
PackageCom.wuyudong.observer;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importandroid.app.Activity;ImportAndroid.content.ContentResolver;ImportAndroid.content.Context;ImportAndroid.database.ContentObserver;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); URI URI= Uri.parse ("content://com.wuyudong.db.personprovider/"); getcontentresolver (). Registercontentobserver (URI, true, new myobserver (new Handler ())); } Private class Myobserver extends contentobserver { PublicMyobserver (Handler Handler) {//handler is a message processor Super(handler); } @OverridePublic void OnChange (boolean selfchange) { //TODO auto-generated Method Stub System.out.println ("Haha, the contents of the database changed!!! "); Super. OnChange (Selfchange); } }}
and modify the code in the Persondbprovider.java:
Publicuri insert (URI uri, contentvalues values) {if(Matcher.match (uri) = =INSERT) { //returns the result set of the querySqlitedatabase db =helper.getwritabledatabase (); Db.insert ("Person",NULL, values); GetContext (). Getcontentresolver (). Notifychange (URI, NULL); } Else { Throw NewIllegalArgumentException ("Path mismatch, cannot perform insert operation"); } return NULL; } @Override Public intDelete (Uri Uri, String selection, string[] selectionargs) {if(Matcher.match (uri) = =DELETE) { //returns the result set of the querySqlitedatabase db =helper.getwritabledatabase (); intresult = Db.delete ("Person", Selection, Selectionargs); Db.close (); if (Result > 0) {getcontext (). Getcontentresolver (). Notifychange (URI, null); } } Else { Throw NewIllegalArgumentException ("Path mismatch, cannot perform delete operation"); } return0; }
This way, each time you click on the button, you will be prompted to change the data of the relevant database.
How the Android Content viewer works