Search code examples
androiddatabaseconcurrencyandroid-contentprovider

Is it possible to use ContentProvider and direct data access to Database in Android? (multiple application case)


I have a similar question as here: Is it possible to use ContentProvider and direct data access to Database in Android? but in my case I want the widget application to be a separate application.

So I have a first Android application that is using a sqlite database using standard SQLiteOpenHelper. this helper is a singleton that is created in my Application onCreate. Works good.

Now I want to make a second application -so a widget application- that needs to access the database as well. So I would like to use a ContentProvider for that.

1) is it safe to do such way or do I have to refactor the main application to use only ContentProvider (clearly would be a showstopper for me!!)

2) if it is ok, how should I design the contentProvider? I guess I will have to reuse my singleton SQLiteOpenHelper and eventually create it in contentProvider.onCreate?


Solution

  • I am also trying to use a ContentProvider and a singleton db object. This appears to be a valid approach.

    Quoting from: http://groups.google.com/group/android-developers/browse_thread/thread/20d8cbf7bf88f6d9

    "Only open the database once. If you are not using a content provider, implement a singleton that takes care of opening the database once for all code in your app.

    "And let me be clear -- there is NOTHING wrong with the content provider never closing the database. Nothing. That is NO reason to avoid using a content provider. In fact it is a very good design approach, and if you aren't using a content provider but directly accessing, I would recommend using the exact same approach for the singleton implementation.

    "If you want to have a content provider AND also elsewhere do direct access to the database, then implement your content provider on top of the singleton that owns the database.

    -- Dianne Hackborn Android framework engineer [email protected]