Today, summarize the use of Asyntask
Principle: Based on asynchronous message processing mechanism
Asynctask is an abstract class, so if we want to use it,
You will have to create a subclass to inherit it.
We can specify three generic parameters for the Asynctask class at the time of inheritance.
1.Parms
Parameters that need to be passed in when executing asyntask can be used in a background task
2.Process
When the background task executes, if you need to display the current progress on the interface, use the generic type specified here as the progress unit
3.Result
When the task finishes executing, if you need to return the result, use the generic type specified here as the return value type
One of the simplest custom asyntask can be written in the following way
Class Downloadtask extends asyntask<void,integer,boolean>{
......
}
The first generic parameter is void, which means that no incoming arguments are required for the background task when executing asyntask
The second generic parameter is integer, which indicates that the integer data is used as the progress display unit
The third generic parameter is Boolean, which means that Boolean data is used to feedback the execution result
The trick to using Asyntask is
The initialization of some interfaces in the OnPreExecute () method is invoked before the background task begins execution.
To perform specific time-consuming tasks in the Doinbackground () method,
UI action in the Onprogressupdate () method,
Perform the finishing touches of some tasks in the OnPostExecute () method.
Finally, if you want to start this task, just write the following code:
New Downloadtask (). Execute ();
The use of Android development Asyntask