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 开发者_StackOverflow社区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!
This is actually very easy.
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.
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.
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.
精彩评论