An example of a content provider and content observer?

I am new to Android development and play with content providers and content watchers. However, it’s hard for me to find examples on the Internet. I played and read about it, but got stuck. Here is what I am trying to do:

I created a small content provider (which I confirmed works and inserts / deletes data in db on the phone). Call it A.apk. Now I want to create a B.apk that will be notified of any updates made to db. Therefore, if new content is created, B will display it, and if the content is deleted, it will be removed from view B.

I am stuck and would like it to be done correctly using best practices. An example would be greatly appreciated!

+5
source share
1 answer

This is actually very simple.

  • Just implement ContentObserver and register it with the database URI to view. In the example of when A uses the B ContentProvider to place data in a B specific database, the B method ContentObserver onChange () will be launched. However, there is a problem if B does not work when changing.

  • Another solution for A is to use B's ContentProvider to insert data into database B, and then send the intent B that new data is waiting.

  • Or, in the implementation of B ContentProvider, it can also be launched by Activity belonging to B.

Depending on the needs of the application and problems, it will be determined which method to use.

+2
source

All Articles