【问题标题】:FATAL EXCEPTION: Intent Service while delete SQLite db file from package致命异常:从包中删除 SQLite db 文件时的意图服务
【发布时间】:2016-01-04 13:49:02
【问题描述】:

我在注销按钮时从 android 应用程序包中删除 Sq-lite 数据库文件,当时还想停止在后台运行但面临 FATAL EXCEPTION: IntentService[helloservice] 错误的服务

这是我的注销按钮代码

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();
        }

【问题讨论】:

  • 我想你忘了写 SQLiteDatabase db = this.getWritableDatabase(); 。在执行任何查询之前,您必须检查这一点。
  • 您在服役期间完成了哪些任务?
  • 我已经在服务中执行了检索数据和更新数据的任务。我可以发布我的服务代码吗。
  • 如果可以的话,请附上您的服务代码
  • 我在上面的帖子中附上了服务代码。

标签: android


【解决方案1】:

您好,请尝试以下步骤,希望对您有用

您需要在执行完与数据库相关的任务后关闭您的数据库 例子

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);

        }
    }

【讨论】:

    【解决方案2】:

    您在这里的预期行为是什么?您想在服务重新启动时重新创建数据库,还是取消任何可能导致服务重新启动的待处理意图?

    【讨论】:

    • 当我删除数据库时,我的服务也想停止 sq-lite 性能。我仍然面临尝试编写只读数据库的错误
    • 您可能有多个挂起的意图在您调用 stopService 后重新启动服务。也许看看这个stackoverflow.com/questions/8358190/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多