Android content observer principle, android's

Source: Internet
Author: User

Android content observer principle, android's

Intercept text messages. For example, when sending a text message, you can read the text message. When the system's text message changes, you yell and send the data to the public message mailbox, our application observes the public message mailbox through the content observer

Obtain the ContentResolver object and call the getContentResolver () function (),

Call the registerContentObserver (uri, policyfordescendents, observer) method of the ContentResolver object. parameters: Uri object, exact uri (true is not accurate, false is accurate), and observer object ContentObserver object

ContentObserver is an abstract class, so we write an internal class to inherit this abstract class and must implement the constructor. The constructor of the constructor will talk about it later and define an internal class MyObserver, the onChange () callback method of the parent class is implemented. This method is called back when the message mailbox changes.

In this callback function, obtain the content of the text message, obtain the last one, and call the moveToFirst () pointer of the Cursor object to point to the last one.

How does the system application send this call? obtain the ContentResolver object and call the policychange (uri, observer) method of the ContentResolver object through the getContentResolver () method. The parameter is defined randomly by uri, observer specifies who handles the default null

Many applications of the system implement notification communication through this public message mailbox mechanism.

Next, let's take a look at the actual situation. Next, we will use the project in "Implementation of Android content providers" as the basis to create a project and add the Code as follows:

Package com. wuyudong. observer; import uri; import android. OS. bundle; import android. OS. handler; import android. app. activity; import android. content. contentResolver; import android. content. context; import android. database. contentObserver; import android. view. menu; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstan CeState); 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 {public MyObserver (Handler handler) {// handler is a message processor super (handler) ;}@ Override public void onChange (boolean selfChange) {// TODO Auto-ge Nerated method stub System. out. println ("haha, the database content has changed !!! "); Super. onChange (selfChange );}}}

Modify the code in PersonDBProvider. java:

Public Uri insert (Uri uri, ContentValues values) {if (matcher. match (uri) = INSERT) {// return the query result set SQLiteDatabase db = helper. getWritableDatabase (); db. insert ("person", null, values); getContext (). getContentResolver (). policychange (uri, null);} else {throw new IllegalArgumentException ("path mismatch, insertion cannot be performed");} return null;} @ Override public int delete (Uri uri, string selection, String [] selectionArgs) {if (matcher. match (uri) = DELETE) {// return the query result set SQLiteDatabase db = helper. getWritableDatabase (); int result = db. delete ("person", selection, selectionArgs); db. close (); if (result> 0) {getContext (). getContentResolver (). policychange (uri, null) ;}} else {throw new IllegalArgumentException ("The path does not match and cannot be deleted");} return 0 ;}

In this way, the prompt that the database data is modified is printed every time you click the button.

Related Article

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: 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.