使用 mayInterruptIfRunning 取消 ASyncTask - 函数调用顺序 -

Mat*_*ieu 5 android android-asynctask

当我调用cancel(false)stop my 时ASyncTask,我发现该onCancelled()函数甚至在我能够检测到任务在doInBackground()使用isCancelled().

我试图将取消函数的参数更改为 true,但这也无济于事。

当我阅读文档时,我的理解是我将能够检测到取消doInBackground(),从该函数返回,并且只有在onCancelled()调用该函数代替onPostExecute().

我错过了什么吗?我是否需要添加更多同步机制以确保事情按我期望的顺序发生?

编辑:

下面是代码的组织方式:

    AsyncTask<Void,Integer,Void>() {
        ProgressDialog mProgressDialog;

        @Override
        protected Void doInBackground(Void... voids) {

                for (int i=0; i<size; i++) {
                    publishProgress(i/100);
                    if (isCancelled()) {
                        Log.d(TAG, "getting out");
                        return null;
                    }
                    // do some database operations here
                }

                // do some house cleaning work also here
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // create the progress dialog if needed
            mProgressDialog = new ProgressDialog(context);
            mProgressDialog.setMessage("Do it!");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setMax(100);
            mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialogInterface) {
                    cancel(false);
                }
            });
            mProgressDialog.setCancelable(true);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.show();

            // create some database here
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            mProgressDialog.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            mProgressDialog.dismiss();
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            Log.d(TAG, "Cancelling !");

            // since we are cancelling the operation, close and delete the database that was being created
            mProgressDialog.dismiss();
        }
    }
Run Code Online (Sandbox Code Playgroud)

所以问题是,现在,即使在onCancelled(). 症状是消息“出去”和“取消!”的顺序。不一致(我也用调试器做到了,似乎cancel()调用直接转到onCancelled()另一个线程仍在运行时。

一个可能的原因可能是我刚刚发现的这条消息......?(我在 Froyo 上运行这个)

更多的...

尽管我将cancel()调用中的标志设置为 false,但我发现该doInBackground()函数没有机会完成或检测到取消(它甚至从未到达 return 语句)

Mat*_*ieu 4

所以我发现了一些事情(我想我明白发生了什么)......

我认为该onCancelled函数将被调用,而不是在onPostExecutedoInBackground 返回之后...而是onCancelled在调用 AyncTask.cancel() 函数时立即调用。

所以我遇到的问题是,使用我的代码,它将关闭并删除线程doInBackground正在处理的数据库。所以大多数时候,该线程只会崩溃(在 logcat 中看不到太多,但大多数时候,它会进行检查if (isCancelled())......

刚刚更改了该任务的组织,现在工作正常。创建一个单独的函数来执行清理工作,doInBackgroundisCancelled返回 true 时将调用该函数。没用onCancelled来做什么...