关于AsyncTask中的cancel方法
来源:互联网 发布:关于烟的网络歌曲 编辑:程序博客网 时间:2024/06/13 22:49
在这里总结一下:
文档地址
Cancelling a task
A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)
这是话意思是
我们可以随时调用 cancel(boolean)去取消这个加载任务,调用这个方法会间接调用 iscancelled 并且返回true 。
当调用cancel()后,在doInBackground()return后 我们将会调用onCancelled(Object) 不在调用onPostExecute(Object)
为了保证任务更快取消掉,你应该在doInBackground()周期性的检查iscancelled 去进行判断。
**注意,我们的oncancelled和onPostExecute一样,都是在UI线程中执行。。。所以当我们想要取消之后,有些界面变化 我们可以在oncancel里面改变UI.
关于cancel方法
public final boolean 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.某些情况下,我们调用cancel(true)可能就会失效
比如 :task已经加载完成,或者 已经取消过一次,或者是其他情况
转自:http://www.educity.cn/develop/1378229.html
- 关于AsyncTask中的cancel方法
- 关于 AsyncTask cancel问题
- AsyncTask cancel
- AsyncTask中cancel方法的误读
- Android AsyncTask cancel()方法的使用
- AsyncTask的cancel方法并没有停止任务
- AsyncTask的cancel方法失效的原因
- AsyncTask 中的 publishProgress 方法
- 关于Future.cancel(mayInterruptIfRunning)方法的参数的问题
- 关于NSOperationQueue的cancel
- 关于Future.cancel(false)
- ADO Cancel 方法
- 关于pthread的cancel point
- Dialog中的cancel和dismiss
- Cancel 方法范例 (VC++/ADO)
- cancel
- CANCEL
- 关于AsyncTask
- JavaScript 开题篇【1】
- ubuntu配置vim为windows下C++编程风格
- Hibernate的单向1-1关联(二)
- AsyncTask
- Java Memory Model基础
- 关于AsyncTask中的cancel方法
- CodeForces 629B Far Relative’s Problem
- Android Design Support Library之CoordinatorLayout和CollapsingToolbarLayout
- 随机过程课中的一个复积分问题
- for循环中变量i始终为length值的分析与解决
- LinearLayout布局的Layout_weight
- 【慕课笔记】5-2 字符流之文件读写流
- nodejs-express-ejs-mongodb-mongoose 错误锦集
- 稀疏表示与匹配追踪