Content Providers give you an excellent way to store and retrieve data. It is primarily used so that multiple applications can access and change the same set of data. Today, I am briefly going to go over how we can keep track of the changes to the data within the Content Provider. There is two basic parts to this which is
Content Provider and
Content Observer. The Content Provider is what the application provides for you to use in order to provide access to the data. The Content Observer gives the application a way to be notified if the data within a specific Content Provider has been changed.
In order for this all to work, you need setup your Content Provider so that when calling a insert method or update method they notify the observers. For the insert method, you simply need to call notifyChange. For the update method, you need get the cursor being used to update the data and call setNotificationUri on that cursor. Both of these will take place right after the insert and update have happened.
Now if you would like to be notified of the changes on this data you will implement a Content Observer. In order to do this you simply have to construct a Content Observer with a given Handler. Once this Content Observer has been constructed you need to register it by calling registerContentObserver. This will make it so that when any change happens it will be reflected in the onChange method of the Content Observer you have just created.
With that you have both pieces and are able to keep track of the changes that are beings made to your data.