自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于AsyncTask中的cancel方法

移動開發(fā) Android
在adroid 應(yīng)用程序中,我們經(jīng)常會用到異步加載。 所以我們也要知道如何取消加載。 比如,當(dāng)用戶點(diǎn)擊圖片加載時,如果用戶可能需要取消加載。 怎樣取消呢 我去百度找了很久也沒找到 。于是就自己去API文檔中查找。

在這里總結(jié)一下:

文檔地址

 

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.)

這是話意思是

 

我們可以隨時調(diào)用 cancel(boolean)去取消這個加載任務(wù),調(diào)用這個方法會間接調(diào)用 iscancelled 并且返回true 。

 

當(dāng)調(diào)用cancel()后,在doInBackground()return后 我們將會調(diào)用onCancelled(Object) 不在調(diào)用onPostExecute(Object)

 

為了保證任務(wù)更快取消掉,你應(yīng)該在doInBackground()周期性的檢查iscancelled 去進(jìn)行判斷。

**注意,我們的oncancel和onPostExecute一樣,都是在UI線程中執(zhí)行。。。所以當(dāng)我們想要取消之后,有些界面變化 我們可以在oncancel里面改變UI.

關(guān)于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.

 

某些情況下,我們調(diào)用cancel(true)可能就會失效

 

比如 :task已經(jīng)加載完成,或者 已經(jīng)取消過一次,或者是其他情況

責(zé)任編輯:張葉青 來源: eoe Android開發(fā)者社區(qū)
相關(guān)推薦

2010-12-01 14:34:59

AsyncTask異步處理任務(wù)Android

2015-10-23 15:22:16

AsyncTask基礎(chǔ)Android

2011-12-02 14:00:21

JavaOOM

2019-11-07 23:48:12

shell腳本getopts

2011-08-08 14:07:49

iPhone開發(fā) 字體

2011-08-15 09:58:25

iPhoneXib文件UITableView

2011-08-16 18:56:11

iPhone開發(fā)Three20

2014-06-05 15:13:21

BGP

2009-12-03 19:06:01

軟路由設(shè)置

2011-08-19 10:35:19

iPhone應(yīng)用Three20

2020-03-02 00:32:08

Python列表for循環(huán)

2011-08-23 15:57:21

Lua元表元方法

2009-12-08 10:01:12

2012-11-20 12:38:29

2011-06-16 11:01:56

PHP繼承

2012-05-14 10:18:54

jQuery

2011-08-24 14:47:55

LUA文本編輯器

2009-11-23 12:50:26

ADSL寬帶路由器設(shè)置

2009-06-17 16:01:28

2009-07-16 16:47:22

jdk的配置Tomcat的配置eclipse與MyE
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號