Android 致命异常:从包中删除SQLite db文件时使用意图服务
我正在从android应用程序包的“注销”按钮中删除Sq lite数据库文件,当时我还想停止在后台运行的服务,但遇到错误致命异常:IntentService[helloservice] 这是我的注销按钮代码Android 致命异常:从包中删除SQLite db文件时使用意图服务,android,Android,我正在从android应用程序包的“注销”按钮中删除Sq lite数据库文件,当时我还想停止在后台运行的服务,但遇到错误致命异常:IntentService[helloservice] 这是我的注销按钮代码 imgBtn_LogOut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
imgBtn_LogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
stopService(new Intent(Filter_Screen.this, MyService.class));
stopService(new Intent(Filter_Screen.this, DownLodProfileSrvice.class));
Log.e("Service ", " Stop !!!");
editor.putBoolean("LOG_EVENT", false);
editor.commit();
actvitiyContext.deleteDatabase(dbhelper.DATABASE_NAME);
Log.e("Database", " Deleted Completeley !!!");
Intent i = new Intent(Filter_Screen.this, Login_Screen.class);
startActivity(i);
finish();
}
});
错误日志
01-04 10:19:06.415 19739-20562/com.example.tazeen.classnkk E/SQLiteLog﹕ (1032) statement aborts at 23: [update ActivityObjectList set DownLoad_Status='1' where imageaudioPath ='560e9df1-a404-47e8-a98b-3d77f0374213.jpg']
01-04 10:19:06.417 19739-20562/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[helloservice]
Process: com.example.tazeen.classnkk, PID: 19739
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1676)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.tazeen.classnkk.MyService.Loaddata(MyService.java:114)
at com.example.tazeen.classnkk.MyService.onHandleIntent(MyService.java:80)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
这是我的服务任务
public void Loaddata() {
db = myDBHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from ActivityObjectList", null);
if (cursor.moveToFirst())
{
do {
imageName = cursor.getString(cursor.getColumnIndex("imageaudioPath"));
str_DownLoadUrl = cursor.getString(cursor.getColumnIndex("DownLoad_Status"));
str_ActiveImageStatus = cursor.getString(cursor.getColumnIndex("ActiveStatus"));
if(str_ActiveImageStatus.equals("1"))
{
if( str_DownLoadUrl.equals("0") && imageName.endsWith(mp3_Pattern))
{
String fileUrl = namespace + "/DownloadFile/FilePathMobile/ATTACHMENT/FileName/"+imageName;
newFolder = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "classnkk_audio");
download_PngFileImgLoader(fileUrl, newFolder , imageName);
db = myDBHelper.getWritableDatabase();
db.execSQL("update ActivityObjectList set DownLoad_Status='1' where imageaudioPath ='" + imageName + "'");
}
if( str_DownLoadUrl.equals("0") )
{
if(imageName.endsWith(png_Pattern) || imageName.endsWith(jpg_pattern) || imageName.endsWith(bmp_pattern) || imageName.endsWith(gif_pattern) || imageName.endsWith(jpeg_pattern))
{
String fileUrl = namespace + "/DownloadFile/FilePathMobile/ATTACHMENT/FileName/"+imageName;
newFolder = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "classnkk_images");
download_PngFileImgLoader(fileUrl, newFolder, imageName);
db = myDBHelper.getWritableDatabase();
db.execSQL("update ActivityObjectList set DownLoad_Status='1' where imageaudioPath ='" + imageName + "'");
}
}
}
}
while (cursor.moveToNext());
}
cursor.close();
}
您好,请尝试下面的步骤,希望它能为您工作 执行数据库相关任务后,需要关闭数据库 范例
cursor.close()
db.close()
公共类DeleteDbBackgroundTask扩展了AsyncTask
{
@凌驾
受保护的void onPreExecute()
{
//在这里停止你的服务
stopService(新意图(Filter_Screen.this,MyService.class));
stopService(新意图(Filter_Screen.this,DownLodProfileSrvice.class));
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…参数)
{
//删除你的数据库代码在这里
deleteDatabase(“您的数据库路径”);
返回null;
}
@凌驾
受保护的void onPostExecute(void避免)
{
编辑器.putBoolean(“日志事件”,false);
commit();
意图i=新意图(Filter\u Screen.this,Login\u Screen.class);
星触觉(i);
完成();
super.onPostExecute(避免);
}
}
您在这里的预期行为是什么?是否要在服务重新启动时重新创建数据库,或取消任何可能导致服务重新启动的挂起意图?我想您忘记写入SQLiteDatabase db=this.getWritableDatabase()代码>。在执行任何查询之前,您必须对此进行检查。您在服务中执行了什么任务?我在服务中的任务中执行了检索数据和更新数据的任务。我可以发布我的服务代码吗?如果可能,请在上面的帖子中附加您的服务代码我附加了服务代码。当我删除数据库时,我的服务也要停止具有sq-lite性能。尽管如此,我仍然面临着尝试编写只读数据库的错误。在调用stopService后,您可能会有多个挂起的意图重新启动服务。也许看看这个?
cursor.close()
db.close()
public class DeleteDbBackgroundTask extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPreExecute()
{
// Stop your service here
stopService(new Intent(Filter_Screen.this, MyService.class));
stopService(new Intent(Filter_Screen.this,DownLodProfileSrvice.class));
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
// Delete your DB code is here
context.deleteDatabase("Your DB Path");
return null;
}
@Override
protected void onPostExecute(Void aVoid)
{
editor.putBoolean("LOG_EVENT", false);
editor.commit();
Intent i = new Intent(Filter_Screen.this, Login_Screen.class);
startActivity(i);
finish();
super.onPostExecute(aVoid);
}
}