gpt4 book ai didi

android - 取消 AsyncTask

转载 作者:行者123 更新时间:2023-11-29 00:37:30 27 4
gpt4 key购买 nike

正在运行的 AsyncTask 的 .cancel(boolean) 方法如何工作?这是文档:

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.

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.

第二段暗示在调用 cancel 后尽快从 doInBackground 方法返回取决于程序员(通过定期检查 isCancelled())。但是在测试中,我的 AsyncTask(doInBackground 方法)似乎会立即停止,即使我不这样做,在调用 cancel 之后也是如此。谁能解释一下? (顺便说一句,当调用取消时,Logcat 没有显示任何事情发生)。

protected Void doInBackground(Void... V) {
for (int v=3;v>0;v--){
publishProgress(v);
try {Thread.sleep(1500);} catch (InterruptedException e) {}
}
return null;
}

此外,无论提供的 mayInrerruptIfRunning 值如何,都会发生这种情况,因此如果可以解释该值的来源,我们也将不胜感激。

最佳答案

有几个核心方法(“阻塞方法”),例如Thread.sleep(),它们自己检查线程中断并抛出异常。

关于android - 取消 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11705086/

27 4 0
文章推荐: android - 如果您有设备 key ,是否可以访问另一个应用程序数据库?
文章推荐: mysql - 将两个临时表合并为一个表,但保留所有列名
文章推荐: php - 根据特定条件将来自 my_MySQL 的值分组显示在单独的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com