First, let's take a look at Asynctask's definition form:
1publicabstractclass Asynctask<params, Progress, result>}
First Asynctask It is an abstract class, consisting of three generic types, with the following meanings:
- Params: It represents the type of the request parameter
- Progress: The type of progress performed on a task
- Result: Returns the type of the result
However, the above three parameters must not necessarily be set to void when not required, and no return type.
Then we look at its execution process, including the following methods:
Execute (params ... params), which is called when we perform an asynchronous operation, indicating that the task is beginning to execute.
protected void OnPreExecute () {}, after invoking the Execute method, the method is executed, it executes in the UI thread, used to initialize some UI space, etc.
Protected abstract Result Doinbackground (params): The method is executed after the execution of the OnPreExecute, it executes in the background, and accepts an array parameter of type Params, is used to request a network, and it returns the result of a type. The method can update the request progress while executing the network request, calling Publishprogress (Progress ... values).
protected void Onprogressupdate (Progress ... values), if the Publishprogress method is called in the Doinbackground method, then the method will be executed, It is performed on the UI thread, constantly changing the progress according to values, to achieve the desired effect.
protected void OnPostExecute (result result), which is executed after the Doinbackground method has been executed, and can be followed by subsequent UI operations based on the results returned by the Doinbackground. This shows that it is working in the UI thread.
Asynctask of Android Source code parsing