AsyncTask中cancel方法的作用是什么
本篇文章为大家展示了AsyncTask中cancel方法的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
成都创新互联专业为企业提供东乃网站建设、东乃做网站、东乃网站设计、东乃网站制作等企业网站建设、网页设计与制作、东乃企业网站模板建站服务,十年东乃做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
文档地址
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 去进行判断。
**注意,我们的oncancel和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)可能就会失效
上述内容就是AsyncTask中cancel方法的作用是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网页标题:AsyncTask中cancel方法的作用是什么
转载源于:http://scyanting.com/article/gejspd.html