Possible Duplicate:
onPostExecute on cancelled AsyncTask
为什么取消AsyncTask
仍然来电onPostExecute
如果文档说应该调用 onCancelled
反而。
这是一个长期存在的错误吗?
就我而言,我 cancel(true)
asynctask,它完成 doInBackground
然后调用 onPostExecutes。
最后它抛出一个
java.lang.InterruptedException
如果这是故意行为,我可以得到这个Exception
无论如何作为一个对象?
最佳答案
如果您阅读 cancel() 方法的文档,您会发现:
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.
调用cancel()会将isCancelled()设置为true。您是否定期在 doInBackground 中检查此方法的返回值?
protected Object doInBackground(Object... x) {
while (/* condition */) {
// work...
if (isCancelled()) break;
}
return null; }
对于 java.lang.InterruptedException 异常,可能有多种原因。
我对你的情况的猜测是你可能在错误的地点/时间调用cancel(),并且你可能没有在doInBackground中定期检查isCancelled(),因此任务成功完成并在onPostExecute () 叫做。
关于android - AsyncTask cancel(Boolean) 调用 onPostExecute -?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428806/