AsyncTask onCancelled(Object)从未在asyncTask.cancel(true)之后被调用; [英] AsyncTask onCancelled(Object) never invoked after asyncTask.cancel(true);

查看:95
本文介绍了AsyncTask onCancelled(Object)从未在asyncTask.cancel(true)之后被调用;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如文档所述,onCancelled(Object)应该在以下情况下调用:

As the documentation says the onCancelled(Object) should be invoked after:

取消(布尔)被调用并且doInBackground(Object [])已经完成.

在我的AsyncTask中,我调用了this.cancel(true);,但从未调用onCancelled(Object)方法.

In my AsyncTask i invoked this.cancel(true); but the onCancelled(Object) method is never called.

仅在此处发布相关代码.

Only relevant code is posted here.

MainActivity.java :

 AsyncTask.Status asyncTaskStatus = new GetHtmlDocument(urlString).execute().getStatus();

AsyncTask :

private class GetHtmlDocument extends AsyncTask<String,Void,HtmlPage>
    {
        private String url;

        /**
         * Constructor.
         *
         * @param url Url to parse from in the web.
         */
        public GetHtmlDocument(String url)
        {
            this.url = url;
        }

        @Override
        protected void onPreExecute()
        {
            Log.d(MainActivity.ASYNC_TASK_TAG, "onPreExecute() called");
        }

        @Override
        protected HtmlPage doInBackground(String... params)
        {
            //android.os.Debug.waitForDebugger();
            Log.d(MainActivity.ASYNC_TASK_TAG, "doInBackground() called");

            if (this.isCancelled())
            {
                return null;
            }
            else
            {


                HtmlPage htmlPage = new HtmlPage(getParsedDocument(this.url));

                return htmlPage;
            }
        }

        /**
         * Runs on the UI thread after doInBackground().
         * The specified result is the value returned by doInBackground().
         * This method won't be invoked if the asynchronous task was cancelled.
         *
         * @param htmlPage
         */
        @Override
        protected void onPostExecute(HtmlPage htmlPage)
        {
            Log.d(MainActivity.ASYNC_TASK_TAG, "onPostExecute() called");

            if (htmlPage.getHtmlDocument() != null)
            {
                this.cancel(true);
            }

            setHtmlPage(htmlPage);
        }

        /**
         * A task can be cancelled at any time by invoking cancel(boolean).
         * Invoking this method will cause subsequent calls to isCancelled() to return true.
         * After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.
         * To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)
         *
         * @param htmlPage
         *
         */
        @Override
        protected void onCancelled(HtmlPage htmlPage)
        {
            Log.d(MainActivity.ASYNC_TASK_TAG, "onCancelled() called");
        }


    }

我调试了该应用程序,并且确定在AsyncTask onPostExecute()方法中调用了this.cancel(true);.

I debugged the app and I know for sure that this.cancel(true); is called in the AsyncTask onPostExecute() method.

推荐答案

这仅仅是因为您在AsyncTask完成其工作后调用this.cancel(true)

This is simply because you are calling the this.cancel(true) after the AsyncTask done its job!

意味着,您可以在AsyncTask在doInBackground中执行其工作时以及状态为完成"后取消它.

Means, you can cancel the AsyncTask, while it is doing its job in doInBackground, and after its status is "finished"

来自文档,

可以通过调用cancel(boolean)随时取消任务. 调用此方法将导致随后对isCancelled()的调用 返回true.调用此方法后,改为onCancelled(Object) onPostExecute(Object)中的将会在之后被调用 doInBackground(Object[])返回.确保取消任务 尽快检查您的返回值 如果可能,从doInBackground(Object[])周期性地isCancelled() (例如在循环内).

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

因此很明显,onCancelled方法是在AsyncTask在doInBackground完成之前执行任务时调用的,而onCancelled(Object)显然是在调用< INSTEAD "时调用的取消事件时onPostExecute的值. cancel(true)方法旨在中断" AsyncTask的操作.

So its clear that the onCancelled method is meant to be called while the AsyncTask is performing task before doInBackground is completed, and onCancelled(Object) is clearly meant to be invoked "INSTEAD" of onPostExecute on an event of cancelling. the cancel(true)method is meant to "interrupt" the operation of AsyncTask.

而且,我想知道为什么要取消任务?您可以简单地检查结果是否为null,并仅在其不为null时执行onPostExecute,如下所示:

And, i would like to know why you want to cancel the task? you could simply check whether the result is null and perform the onPostExecute only if its not null, like this:

@Override
protected void onPostExecute(HtmlPage htmlPage)
{
    if (htmlPage.getHtmlDocument() != null){
       Log.d(MainActivity.ASYNC_TASK_TAG, "onPostExecute() called");
       setHtmlPage(htmlPage);
    }
}

@Override
protected void onPostExecute(HtmlPage htmlPage)
{
    Log.d(MainActivity.ASYNC_TASK_TAG, "onPostExecute() called");
    if (htmlPage.getHtmlDocument() != null){
       return;
    }

    setHtmlPage(htmlPage);
}

这篇关于AsyncTask onCancelled(Object)从未在asyncTask.cancel(true)之后被调用;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆