Getting started with Android: listening for contentprovider data changes

Source: Internet
Author: User
1. Listener contentprovider main step 1. add this to the insert \ update \ Delete method of the contentprovider class. getcontext (). getcontentresolver (). policychange (Uri, null); Uri indicates the listening urinull indicates that the message is sent to anyone;
2. Call the following method in the visitor's class: context. getcontentresolver (). registercontentobserver (Uri, true, new contentobserver (new handler ()));
3. create an internal class to inherit contentobserver and override the following two methods: (1) constructor with handler; (2) Public void onchange (Boolean selfchange ); this function is called when the content provider sends a change signal.
The selfchange parameter is true if it is the cause of self-change; false if it is not self-changed;
2. We have registered the data changes of contentprovider in both test1 and Test2. If contentprovider sends a notification, both test1 and Test2 will receive the notification; Note: There is a very important issue here. If the mainactivity of the test1 and Test2 applications is the same package, if Test2 is run first and then test1 is run, Test2 will stop running, because the test1 application will overwrite the Test2 application, that is, the application cannot receive the notification; Therefore, we need to set the package names of the test1 application and Test2 application to different ones. Here, they are org. xiazdong and org. xzdong; Contentprovider. Java
Package Org. xiazdong. DB; import android. content. contentprovider; import android. content. contenturis; import android. content. contentvalues; import android. content. urimatcher; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android.net. uri; public class personprovider extends contentprovider {private databasehelper helper; private sqlitedatabase dB; private urimatcher matcher = new urimatcher (urimatcher. no_match); @ overridepublic Boolean oncreate () {helper = new databasehelper (this. getcontext (); matcher. adduri ("org. xiazdong. provides. personprovider "," person ", 1); Return true;} @ overridepublic cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder) {return NULL;}/** if the operation set is set, it must start with vnd. android. cursor. * If the operation is not a set, it must start with vnd. android. cursor. start with item **/@ overridepublic string GetType (URI) {return "" ;}@ overridepublic URI insert (URI Uri, contentvalues values) {DB = helper. getwritabledatabase (); Switch (matcher. match (URI) {Case 1: Long rowid = dB. insert ("person", null, values); this. getcontext (). getcontentresolver (). notifychange (Uri, null); // if the data is changed, the system notifies the owner of return contenturis. withappendedid (Uri, rowid); // return the uridefault: Throw new illegalargumentexception ("wrong Uri");} @ overridepublic int Delete (URI Uri, string selection, string [] selectionargs) {return 0 ;}@ overridepublic int Update (URI Uri, contentvalues values, string selection, string [] selectionargs) {return 0 ;}}
Test1 Application
Package Org. xiazdong; import android. app. activity; import android. content. contentresolver; import android. content. contentvalues; import android. database. contentobserver; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class mainactivity extends activity {private button; private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {contentresolver resolver = mainactivity. this. getcontentresolver (); Uri uri = Uri. parse ("content: // Org. xiazdong. provider2/person "); resolver. registercontentobserver (Uri, true, new personobserver (new handler (); contentvalues values = new contentvalues (); values. put ("name", "Zzz"); values. put ("Age", 1); resolver. insert (Uri, values); // insert data to contentprovider}; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) This. findviewbyid (R. id. button); button. setonclicklistener (listener);} private class personobserver extends contentobserver {// listener public personobserver (handler) {super (handler);} // when contentprovier data changes, this function is triggered @ overridepublic void onchange (Boolean selfchange) {super. onchange (selfchange); log. I ("test1", "Data Change ");}}}

Test2 Application

Package Org. xzdong; import android. app. activity; import android. database. contentobserver; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. util. log; public class mainactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Uri uri = Uri. parse ("content: // Org. xiazdong. provider2/person "); this. getcontentresolver (). registercontentobserver (Uri, true, new personobserver (new handler ();} private class personobserver extends contentobserver {public personobserver (handler) {super (handler );} // when contentprovier data changes, this function @ overridepublic void onchange (Boolean selfchange) {super. onchange (selfchange); log. V ("Test2", "Data Change ");}}}

Effect:

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.