Structure
Inheritance relationship
Public abstract class Asynctask extends Object
Java.lang.Object
Android.os.asynctask<params, Progress, result>
class overview
Asynctask can be used appropriately and simply for the UI thread. This class does not require an action thread (thread) to complete the background operation and return the result to the UI.
The definition of an asynchronous task is one that runs on a background thread, and the result is a calculation published on the UI thread. An asynchronous task is defined as three generic types: Params,progress and Result, and four steps: Begin, Doinbackground,processprogress, and end.
Usage
Aysnctask must be inherited for use. Subclasses cover at least one method (Doinbackground (Params ...)) and most often overwrite the other (OnPostExecute (Result).) Here is an example of a subclass:
Once created, a task is very simple to execute:
Generic Types of Asynctask
These three types are used for an asynchronous task, as follows:
1. Params, starting input parameters for task execution
2. Progress, percentage of background task execution
3. Result, results type of the background calculation
In an asynchronous task, not all types are always used. If a type is not used, you can simply use the void type:
4 steps
When an asynchronous task is executed, the task passes through four steps:
1. OnPreExecute () executes immediately after the task is invoked on the UI thread. This step is typically used to set up tasks, such as displaying a progress bar in the user interface.
2. Doinbackground (Params ...), which is called immediately after the background thread executes OnPreExecute (), is used to perform a lengthy background calculation. The parameters of the asynchronous task are also passed to this step. The result of the calculation must be returned in this step and will be passed back to the previous step. You can call Publishprogress (Progress ...) during execution. To update the progress of the task.
3. Onprogressupdate (Progress ...), one call Publishprogress (Progress ...) After calling the UI thread. The execution time is indeterminate. This method is used to display progress in the user interface when the background calculation is still in progress. For example, this method can be used to animate a progress bar or to display records in a text field.
4. OnPostExecute (Result), when the background evaluation ends, invokes the UI thread. The background calculation results are passed as a parameter to this step.
Thread rules
There are some threading rules that must be adhered to, and this class will work correctly:
· The task instance must be created in the UI thread
· Execute (Params ...) Must be called on the UI thread
· Do not manually call OnPreExecute (), OnPostExecute (Result), Doinbackground (Params ...), Onprogressupdate (Progress ...)
· This task executes only once (if the second execution will throw an exception)
Inner class
Enum Asynctask.status
Represents the current state of a task
constructor function
Public Asynctask ()
Creates a new asynchronous task. This constructor must be called on the UI thread.
Public methods
Public Final Boolean cancel (Boolean mayinterruptifrunning)
Attempts to cancel the execution of this task, if the task has ended or has been canceled or cannot be canceled, or for some other reason, will cause this operation to fail, and when this method is called, the method executes successfully and the task is not executed, then the task will no longer execute. If the task has already started and the parameter passed in to Mayinterruptifrunning is true, the thread executing the task will attempt to break the task.
Parameters
Mayinterruptifrunning If True, the executing thread will be interrupted and if false, the executing task thread will be allowed to finish.
return value
Returns true if this task cannot cancel the return False if it has been performed properly
See
IsCancelled ()
Oncancelled ()
Public final Asynctask<params, Progress, result> execute (params ... params)
Performs this task with the specified parameters, and this method returns the task itself, so the caller can have a reference to the task. This method must be called in the UI thread
Parameters
Params task parameters
return value
Examples of Asynctask
Abnormal
IllegalStateException if GetStatus () returns running or finished
Public final Result get ()
Wait for the calculation to end and return the result
return value
Calculation results
Abnormal
Cancellationexception If the calculation is canceled
Executionexception if the calculation throws an exception
Interruptedexception when waiting for a thread to throw an exception
Public final Result get (long timeout, timeunit unit)
Wait for the calculation to end and return the result with the maximum wait Time: timeout (timeout).
Parameters
Timeout calculation wait Time-out
Unit time-out units
return value
Calculation results
Abnormal
Cancellationexception If the calculation is canceled
Executionexception if the calculation throws an exception
Interruptedexception when waiting for a thread to throw an exception
TimeoutException Wait Time timeout
Public final Asynctask.status GetStatus ()
Get the current status of a task
return value
Current status
Public final Boolean iscancelled ()
Returns true if the task is canceled before the task ends correctly, otherwise false
return value
If the task ends correctly, the Cancel task returns true successfully.
See
Cancel (Boolean)
Protected method
Protected abstract Result doinbackground (params ... params)
Override this method to perform a calculation on a background thread, and the parameters in this method are execute (Params ...) 方法的调用这传递的参数,此方法可以调用
for this task. Publishprogress (Progress ...) 在
Ui线程中来更新数据
Parameters
Params parameter of this task
return value
Returns a result that is defined by this task subclass
See
OnPreExecute ()
OnPostExecute (Result)
Publishprogress (Progress ...)
protected void oncancelled ()
This method is called after the Cancel (Boolean) is called in the UI thread
See
Cancel (Boolean)
IsCancelled ()
protected void OnPostExecute (result result)
This method Doinbackground (Params ...) in the UI thread. Called after the method call, the value of the parameter in this method is Doinbackground (Params ...) 的返回值或者当此任务已经被取消或有异常发生时此参数值为空
Null
Parameters
Result by Doinbackground (Params ...) The result of the computed operation.
See
OnPreExecute ()
Doinbackground (Params ...)
protected void OnPreExecute ()
In method Doinbackground (Params ...)调用之前调用
See
OnPostExecute (Result)
Doinbackground (Params ...)
protected void onprogressupdate (Progress ... values)
This method publishprogress (Progress ...) 被调用之后调用,该方法中的参数
in the UI thread. Values 是已经被传递到
ppublishprogress (Progress ...) The parameters in
Parameters
Values progress Indication value
See
publishprogress (Progress ...)
Doinbackground (Params ...)
Protected final void publishprogress (Progress ... values)
When calling Doinbackground (Params ...) This method is called when the calculation is performed in the background, and Onprogressupdate (Progress ...) is triggered whenever this method is called in the UI thread. Execution of the method
Parameters
Values updates the progress value to the UI
See
Onprogressupdate (Progress ...)
Doinbackground (Params ...)