How to monitor data changes in ContentProvider Based on Android

Source: Internet
Author: User

If the visitor of ContentProvider needs to know the data changes in ContentProvider, you can call getContentResolver (). policychange (uri, null) When ContentProvider changes data to notify the visitor registered on this URI.

Copy codeThe Code is as follows: public class PersonContentProvider extends ContentProvider [
Public Uri insert (Uri uri, ContentValues values ){
Db. insert ("person", "personid", values );
GetContext (). getContentResolver (). NotifyChange(Uri, null );
} // Notify the visitor registered on this URI and registered on the insert method}

If the visitor of ContentProvider needs to be notified of data changes, you must use ContentObserver to listen to the data (the data uses the URI description). When the notification of data changes is received, the system will call the onChange () method of ContentObserver.Copy codeThe Code is as follows: public class MainActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Uri uri = Uri. parse ("content: // cn. wordtech. providers. personprovider/person ");
This. getContentResolver (). registerContentObserver (uri, true, new PersonContentdObserver (new Handler ()));
// The third object is the listener object. When the data changes, the object is notified to change accordingly.
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}

Private class PersonContentdObserver extends ContentObserver {

Public PersonContentdObserver (Handler handler ){
Super (handler );
}

@ Override
Public void onChange (boolean selfChange ){
Uri uri = Uri. parse ("content: // cn. wordtech. providers. personprovider/person ");
Cursor cursor = getContentResolver (). query (uri, null, "personid desc limit 1 ");
While (cursor. moveToNext ()){
String name = cursor. getString (cursor. getColumnIndex ("name "));
Log. I ("Name", name );
}
Super. onChange (selfChange );
}}
}

Test application:
Copy codeThe Code is as follows: Button btn = (Button) findViewById (R. id. btn );
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Uri uri = Uri. parse ("content: // cn. wordtech. providers. personprovider/person"); // obtain the content provider based on the Identification name
ContentResolver cr = MainActivity. this. getContentResolver ();
ContentValues values = new ContentValues ();
Values. put ("name", "Livingstone ");
Value. put ("phone", "1101 ");
Values. put ("amount", "1111111111 ");
Cr. insert (uri, values );
}
});

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