Often times, we developers need to update the apps UI based on some changes that happen in the background(with the background I mean, somewhere else). So, Android Framework has this class known as ContentObserver which can be used exactly for this purpose. We can write some code to react if any change has occurred in files or databases.

What is ContentObserver?

Now in this whole post, I will be using this example, so please understand it. Suppose I have a Call Manager/ Call Logs App which shows the user’s there Call history/Call Logs.

So now if the user has opened our app and is on the Call Logs Activity and suddenly a new incoming call arrives. So as soon as there is a new record in Call Logs database, we need to update the UI based on that. This exact problem is solved using Content Observer.

Example: Call Logs Using ContentObserver

How does it work?

First, we need to create a new class which extends the ContentObserver class. In this new class, we override the onChange() method of Content Observer. In these onChange() method, we write the code we want to execute whenever there is a change in the database.

Once, we have our new class ready, we need to register this content observer object in our calling activity. And according to rule, if we register something, we need to make arrangments for unregistering it. So, we will register our Content Observer object in the onResume() method and we will unregister our object in onPause() method.

If we don’t unregister our object, it will lead to memory leak and our calling activity will never be garbage collected.

Steps To Implement ContentObserver

Step 1. Create a subclass of  ContentObserver class.

public class MyObserver extends ContentObserver {
	public MyObserver(Handler handler) {
        	super(handler);
    	}
}

Step 2. Override the onChange() method of the ContentObserver class

public class MyObserver extends ContentObserver {
	public MyObserver(Handler handler) {
        	super(handler);
    	}

	@Override
	public void onChange(boolean selfChange) {
		this.onChange(selfChange,null);
	}

	@Override
	public void onChange(boolean selfChange, Uri uri) {
		//Write your code here
		//Whatever is written here will be 
		//executed whenever a change is made
	}

}

The second method is only available from API level 16 onwards. If any device has API level below 16 and is using your app, it won’t get URI object as a parameter(that is, the first method will be called). So, it is good practice to not rely on the URI parameter. So, we pass URI param as null.

As your can notice, we have to pass a Handler parameter to the constructor of our class. This Handler object is used to execute our onChange() method. So, if we have created our Handler object on the UI thread, then the onChange() method will be called on the UI thread.

Step 3. Initialize our content observer object in our activity.

MyObserver myObserver = new MyObserver(new Handler());

Step 4. Register our content observer to listen to data changes.

getContentResolver().registerContentObserver(URI,true,myObserver);

It has three parameters as we can see. The first parameter is the URI that we want to listen to. The second parameter indicates whether all the changes to URIs that start with the specified URI should trigger our observer or just the changes made to exactly this URI. The third parameter specifies our observer object.

Step 5. Add code to unregister our content observer object.

getContentResolver().unregisterContentObserver(myObserver);

Usually, we use onResume() method to register and onPause() method to unregister. If we don’t unregister our object, it will lead to memory leak and our calling activity will never be garbage collected.

Downside Of ContentObserver

The problem with content observers is that it just triggers when there is a change in data, but the problem is we do not get additional information about the changes made. We need to go through all the records to find out the change that was made. If only a few records were changed and we need to go through all the records to this out, it will be a waste of resources which is a big problem in the mobile environment.

As you have seen, it is pretty easy to implement ContentObserver in our Android app. This helps us react to changes made to data provided by the content provider for the specified URI.

Pin It on Pinterest

Liked it? Your Friends will also like it!

Share this post with your friends and spread the knowlegde.

Liked it? Your Friends will also like it!

Share this post with your friends and spread the knowledge!