您好,登錄后才能下訂單哦!
本篇文章為大家展示了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方法的作用是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。