ContentObserver and ContentObservable | Android

This article is the extension of Observer Pattern. In this we will try to relate this pattern with Android framework so that it would be easy for Android enthusiasts to relate better. If you are seeking for some more deep basic understanding on Observer Pattern, you can read this.

Subject or ObservableThe object which is being watched.
Class Signaturepublic class ContentObservable extends Observable<ContentObserver> 
Package Detailsandroid.database.ContentObservable
Subject or ObserverThe objects which are watching the state changes.
Class Signaturepublic abstract class ContentObserver extends Object.
Package Detailsandroid.database.ContentObserver

Register your content observer to listen for changes

Do not forget to unregister your content observer

The URIs you can observe

Some common major examples are below:

UserDictionary.Words.CONTENT_URI
ContactsContract.Contacts.CONTENT_URI
Contacts.CONTENT_URI
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
FeedConfig.CONTENT_URI

Programming Example

As soon as any transaction on the contact db will take place like if contact will be created, deleted or modified all cases, we will get notification in MyObserver class.

So the simple steps would be
1) Need to register by getContentResolver().registerContentObserver.
2) Need to pass proper parameters, then only proper observation is possible.
3) Since ContentObserver is abstract class, so need to inherit it.

Some more examples

One Comment

  1. Pingback: Observer Design Pattern using Java Android - Technical Jungle

Leave a Reply

Your email address will not be published. Required fields are marked *