AsyncTask的从未执行onPostExecute [英] AsyncTask never executes onPostExecute

查看:125
本文介绍了AsyncTask的从未执行onPostExecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行以下AsyncTask的:

I am trying to execute the following AsyncTask :

private class TimeUpdateTask extends AsyncTask<List<mObject>, Integer, Void> {

        @Override
        protected Void doInBackground(List<mObject>... params) {
            mObject o;
            int i;
            int numChecked = 0;

            List<mObject> mObjects = params[0];
            while(true)
            {   
                if (isCancelled())
                {
                    Log.w("TAG", "task interrumped");
                    return null;
                }

                    for (i=0 ; i < mObjects.size() ; i++)
                    {
                        o = mObjects.get(i);
                        if (!o.isChecked())
                        {
                            o.calculateProgress();
                            if (o.isChecked())
                            {
                                numChecked++;
                            }
                        }
                    }
                publishProgress(numChecked);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }

            }
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void onProgressUpdate(Integer... param){
            int progressCompleted = param[0];

            ((ArrayAdapter<mObject>) EventListView.this.EventView.getAdapter()).notifyDataSetChanged();

            mMain.setProgressBarCompleted(progressCompleted);
        }

        /*This should execute*/
        @Override
        protected void onPostExecute(Void result) {
            Log.d("TAG","End onPostExecute");
         }

    }

所以,当我打电话取消(假)在此任务中, onPostExecute 应执行,但它永远不会是。

So when I call cancel(false) on this task, onPostExecute should be executed but it never is.

有没有在code或别的什么问题? 我已经看过了很多SO答案,似乎有#5 的AsyncTask 游逛是正常的,即使你只使用一个(在我的情况),在的时刻。

Is there any problem in the code or something else? I have looked at a lot of answers in SO and it seems that having #5 AsyncTask hanging around is normal, even if you are using only one (as in my case) at the moment.

推荐答案

有1大块缺失在这里,你必须了解。

There is 1 large piece missing here that you must learn about.

从开发文档(见下文粗体文本):的http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

from the developer docs (see bold text below): http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

试图取消对此任务的执行。这种尝试如果将失败   任务已经完成,已经被取消,或无法   被取消某些其他原因。如果成功的话,并且此任务有   没有取消的时候被调用开始,这个任务不应该运行。如果   任务已经启动,则mayInterruptIfRunning参数   判定执行此任务的线程是否应   中断在试图停止任务。

Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

调用此方法将导致onCancelled(对象)被调用   doInBackground后在UI线程(对象[])的回报。 电话   这种方法保证了onPostExecute(对象)是永远不会被调用。   调用此方法后,你应该检查返回的值   isCancelled()定期从doInBackground(对象[])完成   任务尽早

Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns. Calling this method guarantees that onPostExecute(Object) is never invoked. After invoking this method, you should check the value returned by isCancelled() periodically from doInBackground(Object[]) to finish the task as early as possible.

这篇关于AsyncTask的从未执行onPostExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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