views:

19

answers:

1

The android content provider has methods for the normal suspects {insert, delete, query, update} but if I want to send control messages there is no obvious mechanism. Suppose one of the tables managed by the needs to be reset, cleared and reloaded. How can this be done? I have several approaches each of them seems a bit hackish.

1) create an activity(or service/receiver) to to the job. The activity does a bulk delete and insert. This seems like the least hackish but when the content provider starts the database helper creates the tables and initializes them.
It seems inefficient to duplicate the initialization code in the content provider's database helper and in a separate activity.

2) hijack the content provider api. A dummy table could be created where the insert (or update/query/delete) method would invoke otherwise inaccessible methods.

3) use multiple inheritance to make the content provider also an service (or activity). This is probably closest to what I want but I don't know how to do this. It feels dangerous, especially if it were an activity.

4) have a service content-provider pair Similar to (3) but hopefully without the warts. The service would have aidl for doing all the control kinds of things. Such as, loading lookup tables, clearing tables, purging expired tuples, ... If this were done, the service how would the service be started? by the content provider?

+1  A: 

3) is not possible in Java.

4) would simply dump the ContentProvider and use the API supplied by the service.

There might also be:

5) Have the ContentProvider register a BroadcastReceiver via getContext().registerReceiver(), and send broadcasts to it for the different operations

I haven't tried that, but assuming the Context returned by getContext() supports it, it should work.

CommonsWare
Yes the user of a BroadcastReceiver works very nicely.
phreed
@phreed: Sweet! I know the `Context` handed to a `BroadcastReceiver` is somewhat limited in what it can do, so I was concerned that the one given to a `ContentProvider` might have similar problems. Glad to hear it worked for you!
CommonsWare

related questions