亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關於cancelAllTasks疑問

1.task.cancel(false),記得初級課程時,是將它標註成true,再由isCanceled判斷是否要繼續

2,呼叫cancel的方法,就能讓task停止嗎?只是標註該task要取消,然後在task中去檢查,當isCanceled為true時終止目前的工作,讓task能提早結束

正在回答

2 回答

1,什么時候檢查,這個就看你自己控制了,就跟結束一個線程一樣,需要自己判斷什么時候去檢查flag。在你認為恰當的時機。你可以每隔一行檢查一次。。。

2,onCancelled(Object)是AsyncTask的回調,帶on的么。

Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns.

調用cancel方法會導致onCancelled(Object)執行,onCancelled在UI線程中,在doInBackgourn返回之后。

這是onCancelled的文檔:

protected void onCancelled(Bitmap bitmap)

Description copied from class: AsyncTask?

Runs on the UI thread after cancel(boolean) is invoked and doInBackground(Object[]) has finished.

The default implementation simply invokes onCancelled() and ignores the result. If you write your own implementation, do not call super.onCancelled(result).

Overrides:

onCancelled in class AsyncTask

這個方法是在UI線程執行,cancel調用后并且doInBackground執行完成后執行。也就是說如果需要在task取消的時候做一些處理,那么就在這里進行。

是要設置檢查點讓doInBackground結束,這個方法才能執行。不過需不需要重寫這個方法完全看具體需求了。

0 回復 有任何疑惑可以回復我~

public final boolean cancel (boolean mayInterruptIfRunning)

Added in API level 3


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.

Parameters

mayInterruptIfRunning

true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.

Returns

false if the task could not be cancelled, typically because it has already completed normally; true otherwise

See Also

isCancelled()

onCancelled(Object)

這是方法的文檔,寫的很清楚了。跟你說的差不多。

0 回復 有任何疑惑可以回復我~
#1

eric_0023 提問者

thanks 1 在doINBackground週期去檢查,多久檢查一次較恰當或有其它方法 2 onCancel是當task背景執行提早結束時會被呼叫,不過前題還是要設檢查點break工作來結束task,是這樣嗎? 如果是這樣 問題1 要怎麼解決
2015-06-11 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android必學-異步加載
  • 參與學習       50616    人
  • 解答問題       333    個

了解Android中的異步加載處理方法,這是面試問的最多的知識點

進入課程

關於cancelAllTasks疑問

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號