I would like to use a SQLite database to store data in an Android application. No problem. But at predefined intervals (every day at 10, 14, 18 and 22 o'clock), I want to run an IntentService
that requires access to the database as well. And here comes the problem:
When the user is using the application while the IntentService
is started, there will be a problem because there's one database connection active in the Activity
and one is to be established in the IntentService
. So the database will be locked and the IntentService
will fail, right?
How an I achieve that, in these cases, the IntentService
just waits for the Activity
to finish the current operation (SELECT
, UPDATE
, etc.) and then runs normally?
In my Activity, I open the database connection (via SQLiteOpenHelper
) in onCreate
and close it in onDestroy
. Does this mean that there's no chance for the IntentService to work on the database in the meantime?
So the database will be locked and the IntentService will fail, right?
If they perform operations at the same time on separate SQLiteDatabase
objects, yes.
How an I achieve that, in these cases, the IntentService just waits for the Activity to finish the current operation (SELECT, UPDATE, etc.) and then runs normally?
Use a single SQLiteOpenHelper
instance for both the activity and the service, whether via a singleton, custom Application
object, or a ContentProvider
.