Here is a summary:
Document address
Cancelling a task
A task can be canceled at any time by invoking cancel (boolean ). invoking this method will cause subsequent callto 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 canceled as quickly as possible, you shoshould always check the return value of isCancelled () periodically from doInBackground (Object []), if possible (inside a loop for instance .)
This means
We can call cancelboolean at any time) to cancel the loading task. Calling this method indirectly calls iscancelled and returns true.
After cancel () is called, after doInBackground) return, we will call onCancelled (Object) instead of onPostExecute (Object)
To ensure faster task cancellation, you should check iscancelled periodically in doInBackground for judgment.
** Note that our oncancel and onPostExecute are both executed in the UI thread... So when we want to cancel the operation, we can change the UI in oncancel in some interface changes.
Cancel Method
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 canceled, or cocould not be canceled for some other reason. if successful, and this task has not started when cancel is called, this task shoshould never run. if the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task shocould be interrupted in an attempt to stop the task.
In some cases, canceltrue may be invalid.
For example, a task has been loaded, canceled, or otherwise