Saturday, May 16, 2015

Android ContentObserver Tutorial for dummies

Hi all,

The ContentObserver it is the most easiest way to get a notification about changes according Uri.

Here is a step by step tutorial How to implement ContentObserver in Android:

1. Create an interface for ContentObserver callback methods:

 public interface ContentObserverCallback {  
   void update();  
 }  

2. Create a class with ContentObserver logic:

 public class MyContentObserver extends ContentObserver {  
   private static final String LOG_TAG = "MyContentObserver";  
   private ContentObserverCallback contentObserverCallback;  
   public MyContentObserver(ContentObserverCallback contentObserverCallback) {  
     // null is totally fine here  
     super(null);  
     this.contentObserverCallback = contentObserverCallback;  
   }  
   @Override  
   public void onChange(boolean selfChange) {  
     this.onChange(selfChange, null);  
   }  
   @Override  
   public void onChange(boolean selfChange, Uri uri) {  
     // this is NOT UI thread, this is a BACKGROUND thread  
     Log.i(LOG_TAG, "Received onChange");  
     contentObserverCallback.update();  
   }  
 }  

3. Create an Activity or a Fragment with implementation of a ContentObserver callback interface:

 public class MainActivityFragment extends Fragment   
        implements ContentObserverCallback { ... }  

4. Implement method for a ContentObserver callback:

   /**  
    * this is a callback method for a content observer  
    * runs NOT in UI thread  
    */  
   @Override  
   public void update() {  
     getActivity().runOnUiThread(new Runnable() {  
       @Override  
       public void run() {  
         // TODO add here any needed logic  
         // set a count number to a TextView  
         txtCount.setText(String.valueOf(++count));  
       }  
     });  
   }  

5.  Register Content Observer object for some Uri. Do it in onResume:

   @Override  
   public void onResume() {  
     super.onResume();  
     if (myContentObserver == null) {  
       myContentObserver = new MyContentObserver(this);  
     }  
     // register ContentObserver in onResume  
     getActivity().getContentResolver().  
         registerContentObserver(  
             // observable uri  
             MyContentProvider.uriTest,  
             true,  
             myContentObserver);  
   }  

6. Unregister Content Observer object. Do it always in onPause to prevent memory leak:

   @Override  
   public void onPause() {  
     super.onPause();  
     // always call unregisterContentObserver in onPause  
     // skipping this call will produce memory leak  
     getActivity().getContentResolver().unregisterContentObserver(myContentObserver);  
   }  

7. Inside ContentProvider in methods like update(), insert() or delete() call notifyChange()

   @Override  
   public int update(Uri uri, ContentValues values, String selection,  
            String[] selectionArgs) {  
     // send update notification to observer  
     getContext().getContentResolver().notifyChange(uri, null);  
     return 1;  
   }  


That's all! Next time any changes with an observable Uri will send notification to your ContentObserver implementation.

Here is a link to the GitHub repository with full code example: ContentObserverTutorial


Thank you

No comments:

Post a Comment