在我的应用android中,我可以从以下位置访问我的sqlite数据库:
1)每60秒为android服务一次.
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;
}
在How to check if a service is running on Android?中建议
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)
解决方法:
您可以覆盖服务的onDestroy()事件,然后尝试访问该服务上的数据库:
@Override
public void onDestroy() {
// *** access to my database ***
super.onDestroy();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。