有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AsyncTask onCancelled(对象)在AsyncTask之后从未调用。取消(真);

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

cancel(布尔)被调用,doInBackground(对象[])已完成

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

这里只发布了相关代码

主要活动。java

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

异步任务

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)
        {
            //安卓.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{}方法中调用了this.cancel(true);


共 (1) 个答案

  1. # 1 楼答案

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

    意味着,您可以在异步任务在doInBackground中执行其工作时,在其状态为“完成”后取消该任务

    根据文件

    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方法是在异步任务在doInBackground完成之前执行任务时调用的,而onCancelled(Object)显然是在取消事件时被onPostExecute调用的。cancel(true)方法用于“中断”异步任务的操作

    我想知道你为什么要取消这项任务?您只需检查结果是否为null,并仅在其不为null时执行onPostExecute,如下所示:

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