Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 使用mayInterruptIfRunning取消ASyncTask – 函数调用顺序 –大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我调用cancel(false)来停止我的ASyncTask时,我发现即使在我能够使用isCancelled()在doInBACkground()中检测到任务被取消之前,也会调用onCancelled()函数.

我试图将cancel函数的参数更改为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();
        }
    }

所以现在的问题是,即使在onCancelled()中删除数据库,仍然会对数据库执行一些操作.
症状是消息“退出”和“取消!”的顺序是不一致的(我也用调试器做了,似乎在另一个线程仍在运行时,cancel()调用直接转到onCancelled().

一个可能的原因可能是this message我刚发现……? (我在Froyo上运行这个)

更多…

然我将cancel()调用中的标志设置为false,但我发现doInBACkground()函数没有机会完成或检测取消(它甚至永远不会返回到return语句)

解决方法

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

我认为在doInBACkground返回之后将调用onCancelled函数而不是onPostExecute …而是在调用AyncTask.cancel()函数调用onCancelled.

所以我遇到的问题是,使用我的代码,将关闭删除doInBACkground线程正在处理数据库.所以大部分时间,该线程都会崩溃(在logcat中没有看到太多,但大多数时候,它会检查if(isCancelled())…

刚刚改变了这项任务的组织,现在工作正常.创建一个单独的函数来执行清理,当isCancelled返回true时,doInBACkground将调用函数.没有使用onCancelled任何东西……

大佬总结

以上是大佬教程为你收集整理的android – 使用mayInterruptIfRunning取消ASyncTask – 函数调用顺序 –全部内容,希望文章能够帮你解决android – 使用mayInterruptIfRunning取消ASyncTask – 函数调用顺序 –所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。