Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content Provider and Content Observer Example?

I am new to Android development and playing with content providers and content observers. However I am having trouble finding examples online. I have been playing and reading about this but have become stuck. Here is what I am trying to do:

I have created a small content provider (that I have confirmed is working and inserting/deleting data in a db on the phone). We will call this A.apk. Now I want to create a B.apk that will be notified with any updates done to the db. So if new content is created B will display it and if content is delete it will be removed from B's view.

I am stuck and would love to see how this is done correctly using best practices. An example would be much appreciated!

like image 647
jjNford Avatar asked Nov 04 '22 14:11

jjNford


1 Answers

This is actually very easy.

  1. Just implement a ContentObserver and register it with the URI of the database to watch. In the example about when A uses B's ContentProvider to put data into B's defined database, B's ContentObserver's onChange() method will be triggered. There is a problem however if B is not running when a change is made.

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

  3. Or in the implementation of B's ContentProvider it could start and Activity belonging to B.

Depending on what the application's needs and concerns are will determine which method to use.

like image 87
jjNford Avatar answered Nov 09 '22 05:11

jjNford