The Android UI thread is primarily responsible for handling user key events, User touch screen events, and screen drawing events, so other developer actions should not and cannot block the UI thread, or the UI will become unresponsive-the user feels very bad. (in short, developers need to keep in mind that there are no time-consuming actions in the UI thread).
To prevent the UI thread from losing its response, Android recommends putting the time-consuming action on the new thread, but the new thread may also need to dynamically update the UI component: for example, you need to get a Web page from the Web, and then textview its source code, you should connect the network, The operation to get network data is done in the new thread. The problem is that after acquiring network data, the new thread does not allow the UI component to be updated directly.
To solve the problem that the new thread cannot update the UI components, Android offers several solutions:
1. Use handler to implement communication between threads.
2, Activity.runonuithread (Runnable).
3, View.post (Runnable).
4, view.postdelayed (Runnable,long).
The next three ways may cause programming to be a little cumbersome, while asynchronous tasks (Asynctask) can further simplify this kind of operation.
Asynctask<> is an abstract class that is typically used for inheritance and requires the following three generic parameters to be specified when inheriting asynctask.
Relatively asynctask is lighter, and is suitable for simple asynchronous processing without the need for threading and handler.
Asynctask<params,progress,result> is an abstract class that defines the following three types of generics.
1. Params: Type of input parameter to start task execution
2. Progress: The type of progress value completed by the background task
3. Result: The type of the result returned when the background execution task is completed.
Use Asynctask as long as the following three steps.
1. Create a subclass of Asynctask and specify a type for three generic parameters. If a generic parameter does not require a specified type, it can be specified as void
2, according to the needs, the following methods to achieve Asynctask:
2.1. Doinbackground (Params ...): Overriding this method is the task that the background thread will complete. The method can call Publishprogress (Progress. Values) method to update the execution progress of the task.
2.2, Onprogressupdate (progress...values): The method is triggered when the publishprogress () method is called in the Doinbackground () method to update the execution progress of the task.
2.3, OnPreExecute (): This method will be called before the background time-consuming operation is performed. Typically, this method is used to perform some initialization work, such as displaying a progress bar on the interface.
2.4, OnPostExecute (result result): When Doinbackground () completes, the system automatically calls the OnPostExecute () method and passes the return value of the Doinbackground () method to the method.
3. Call the instance of the Asynctask subclass execute (params ... params) to begin the time-consuming task.
Use of Asynctask is subject to the following rules
1. You must create an instance of Asynctask in the UI thread
2. The Execute () method of Asynctask must be called in the UI thread
3. Asynctask OnPreExecute (), onpostexecute (result result), doinbackground (params ... params), onprogressupdate ( Progress...values) method, which should not be called by the programmer code, but is called by the Android system.
4, each asynctask can only be executed once, multiple calls will throw an exception.
Asynchronous Task (Asynctask)