gpt4 book ai didi

android - AsyncTask onCancelled() 和 AsyncTask.doInBackground() 的执行时机

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:18 25 4
gpt4 key购买 nike

根据 AsyncTask.cancel method 的 Android 引用, onCancelled() 和 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

但是,查看我的 logcat,我可以看到 onCancelled() 方法在 doInBackground() 方法返回之前执行。

07-10 12:38:57.000: VERBOSE/AsyncTask(7473): doInBackground entered
07-10 12:38:57.000: VERBOSE/AsyncTask(7473): AsyncTask attempting to take the lock
07-10 12:38:57.000: VERBOSE/AsyncTask(7473): AsyncTask got the lock
07-10 12:38:57.420: VERBOSE/AsyncTask(7473): Start Item[0].state = 0
07-10 12:38:57.933: VERBOSE/AsyncTask(7473): onProgressUpdate entered
07-10 12:38:57.940: VERBOSE/AsyncTask(7473): onProgressUpdate exited
07-10 12:38:58.320: VERBOSE/(7473): onCancelListener cancelling AsyncTask
07-10 12:38:58.400: VERBOSE/AsyncTask(7473): onCancelled entered
07-10 12:38:58.400: VERBOSE/AsyncTask(7473): onCancelled exited
07-10 12:38:58.560: VERBOSE/AsyncTask(7473): Started checking file URL
07-10 12:38:58.601: VERBOSE/FileHost(7473): checkFile entered
07-10 12:38:58.641: VERBOSE/FileHost(7473): checkFile checking URI
07-10 12:38:58.691: DEBUG/dalvikvm(7473): threadid=19 wakeup: interrupted
07-10 12:38:58.710: VERBOSE/AsyncTask(7473): AsyncTask released the lock
07-10 12:38:58.710: VERBOSE/AsyncTask(7473): doInBackground exited

使用调试器并在 onCancelled() 方法和 doInBackground() 方法结束处设置断点,我还可以看到在 doInBackground() 结束之前调用了 onCancelled()。

是否有某种方式导致我在 AsyncTask 中错误编码某些内容,导致 Android 引用和我的应用程序行为之间存在这种行为差异?

编辑为 Gallal 添加一些代码:

@Gallal,Activity 包含这段代码。

private class OnCancelListener implements AddUrlDialog.CancelListener {
@Override
public void cancel() {
if (addUrlInProgress == true) {
addUrlInProgress = false;
Log.v(TAG, "onCancelListener cancelling AsyncTask");
addUrlControl.stopUpdates(true);
AddUrlDialog.dismiss();
}
}
}

AsyncTask.cancel 在 addUrlControl.stopUpdates() 方法中调用。

public void stopUpdates(boolean cleanupLists) {
if (asyncTaskExited != true) {
cancelRequest = true;
addUrlAsyncTask.cancel(true);
//TEST httpRequest.abort(); // Also sends an abort to the HTTP request
}
}

AsyncTask doInBackground 方法如下所示。

@Override
protected Void doInBackground(Void... v) {
Log.v(TAG, "doInBackground entered");

netConn = addUrlControl.myApp.getNetConn();
client = netConn.getHttpClient();

try {
doInBackgroundBody();
} catch (Throwable t) {
Log.e(TAG, "doInBackgroundBody threw an exception: ", t);
} finally {
addUrlControl.myApp.releaseNetConn();
}

Log.v(TAG, "doInBackground exited");
return null;
}

最佳答案

我可以确认(正如你们中的一些人已经说过的那样)有一个 bug in the Android source for 2.x versions这使得 onCancelled()cancel() 之后和 doInBackground() 完成之前被调用。

因此,如果您在 onCancelled() 中进行清理,您实际上会在 AsyncTask 运行的同时执行此操作,这会使您的应用程序崩溃!这让我们改变设计...

希望对您有所帮助!

关于android - AsyncTask onCancelled() 和 AsyncTask.doInBackground() 的执行时机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6641322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com