AsyncTask 从不执行 onPostExecute [英] AsyncTask never executes 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");
}
}
所以当我在这个任务上调用 cancel(false)
时,onPostExecute
应该被执行,但它从来没有.
So when I call cancel(false)
on this task, onPostExecute
should be executed but it never is.
是代码有问题还是别的什么?我在 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(Object)被调用在 doInBackground(Object[]) 返回后的 UI 线程上.呼叫此方法保证永远不会调用 onPostExecute(Object).调用此方法后,您应该检查返回的值isCancelled() 周期性地从 doInBackground(Object[]) 完成尽早完成任务.
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屋!