First, Knowledge introduction
1. ContentProvider is the content provider
Contentresolver is the content resolver (manipulating the data provided by the content)
Contentobserver is a content observer (observing the data changes provided by the content provider)
2, Contentobserver need contentresolver to register.
Resolver.registercontentobserver (uri,true,observer);
①uri (first parameter): The URI of the listener listening on ContentProvider
②notifyfordescendents (second parameter): set to True. (If the requested URI is CONTENT://ABC, the URI is content://abc/xyz data changes will be detected.)
③observer: listener instance.
3, using the OnChange method in observer, when the data is changed, the callback method is automatically executed.
second, the project practice"Step"
① Defining observer listeners
② Get to contact Contentresolver
③ registered observer.
④ Important point: Add access rights (dynamic also)
⑤ Logoff Observer
"Project Structure"
"Mainactivity"
Hint: This observer is written in the form of an inner class. The code has only this part, and the others do not need to be added.
1 ImportAndroid. Manifest;2 ImportAndroid.content.ContentResolver;3 ImportAndroid.content.pm.PackageManager;4 ImportAndroid.database.ContentObserver;5 ImportAndroid.net.Uri;6 ImportAndroid.os.Handler;7 Importandroid.provider.ContactsContract;8 ImportAndroid.support.v4.app.ActivityCompat;9 Importandroid.support.v7.app.AppCompatActivity;Ten ImportAndroid.os.Bundle; One ImportAndroid.util.Log; A ImportAndroid.widget.Toast; - - Public classMainactivityextendsappcompatactivity { the -Contentresolver resolver =NULL; -Observer Observer =NULL; - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); A at //Dynamic permission requests (also ask for permission in the Androidmanifest file) - if(Activitycompat.checkselfpermission ( This, -Manifest.permission.READ_CONTACTS)! = - packagemanager.permission_granted); - Activitycompat.requestpermissions ( -Mainactivity. This, in Newstring[]{Manifest.permission.READ_CONTACTS -},0); to + //Instantiate Observer -Observer =NewObserver (NewHandler ()); the //Get resolver *Resolver =getcontentresolver (); $Uri uri =ContactsContract.Contacts.CONTENT_URI;Panax Notoginseng //Register Observer -Resolver.registercontentobserver (URI,true, observer); the + } A the classObserverextendscontentobserver{ + - PublicObserver (Handler Handler) { $ Super(handler); $ } - - @Override the Public voidOnChange (BooleanSelfchange) { - Super. OnChange (selfchange);WuyiToast.maketext (mainactivity. This, the"Contact List has changed", Toast.length_short). Show (); - //Add Toast in onchange method for easy observation Wu } - } About $ @Override - Public voidOndetachedfromwindow () { - Super. Ondetachedfromwindow (); - //unregister observer in activity A Resolver.unregistercontentobserver (Observer); + } the}
"effect" has just deleted a contact information
Summary
①contentobserver is able to detect data changes provided by ContentProvider in a timely manner.
②contentobserver can communicate not only with the system, but also from the programs that they create by contentobserver,contentresolver
Android contenobserver Monitor Contact data changes