在我的应用程序 android 中,我可以从以下位置访问我的 sqlite 数据库:
1)服务android,每60秒。
2) 按下按钮后 (onclik)
如果我在服务访问数据库时按下按钮,则会发生错误。
所以我在 onclickListener 中插入,在访问数据库之前,调用 stopService()
但是冲突仍然存在,因为即使我在访问数据库之前调用 stopService() ,服务也会在此之后停止!
我试过了,使用:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
在访问数据库之前,我尝试以这种方式调用此方法:
while(isMyServiceRunning())
{
// do nothing
}
// *** access to my database ***
我可以做些什么来确保在访问数据库之前停止我的服务?
错误是:
10-30 21:05:43.360: E/SQLiteDatabase(13960): Failed to open the database. closing it.
10-30 21:05:43.360: E/SQLiteDatabase(13960): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2115)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:984)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:753)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
10-30 21:05:43.360: E/SQLiteDatabase(13960): at host.database.DB_DatabaseManager.open(DB_DatabaseManager.java:134)