Address: http://blog.csdn.net/cjjky/article/details/6684959
When developing Android mobile clients, multithreading is often used for operations. We usually place time-consuming operations in separate threads to avoid
It occupies the main thread and brings a poor user experience. However, the main thread (ui thread) cannot be operated in the Child thread, and the UI thread cannot be operated in the Child thread.
An error occurs. Therefore, Android provides a handler class to update the UI thread in the Child thread, and uses the message sending mechanism to update the UI and present it to the user.
User. This solves the problem of updating the UI of the sub-thread. However, time-consuming task operations always start some anonymous sub-threads. Too many sub-threads bring to the System
A huge burden brings about some performance problems. Therefore, Android provides a tool class asynctask, which is called asynchronous task execution. This
Asynctask is born to process some time-consuming tasks in the background, which brings users a good user experience, and looks elegant in programming syntax.
More, asynchronous operations can be completed without the child thread and handler and the user interface is refreshed.
First, let's get to know about the Android. OS. asynctask class:
* The Android asynctask class encapsulates inter-thread communication and provides a simple programming method for background thread and UI thread communication: Background thread
And notifies the UI thread of the operation result.
* Asynctask is an abstract class. asynctask defines three generic types: Params, SS, and result.
* Input parameters for Params startup task execution, such as the URL of the HTTP request.
* Percentage of progress SS background tasks executed.
* The result returned by the backend execution task, such as string and integer.
* Asynctask is executed in four steps. Each step corresponds to a callback method. developers need to implement these methods.
* 1) inherit asynctask
* 2) implement one or more of the following methods defined in asynctask
* Onpreexecute (), which is called by the UI thread before the actual background operations are performed. You can make some preparations in this method, such
Displays a progress bar or some controls are instantiated. This method does not need to be implemented.
* Doinbackground (Params...) will be executed immediately after the onpreexecute method is executed. This method runs in the background thread. The primary responsibilities are as follows:
Perform time-consuming background processing. You can call publishprogress to update the real-time task progress. This method is an abstract method, subclass
Required.
* Onprogressupdate (Progress...), after the publishprogress method is called, the UI thread will call this method to display
Progress, for example, through a progress bar.
* Onpostexecute (result). After doinbackground is executed, the onpostexecute method will be called by the UI thread.
This method is passed to the UI thread and displayed to the user on the interface.
* Oncancelled (), called when the user cancels the thread operation. It is called when oncancelled () is called in the main thread.
To correctly use the asynctask class, the following guidelines must be followed:
1) The task instance must be created in the UI thread.
2) The execute method must be called in the UI thread.
3) do not manually call onpreexecute (), onpostexecute (result), doinbackground (Params ...), onprogressupdate (Progress ...) these methods need to be instantiated in the UI thread to call this task.
4) The task can only be executed once. Otherwise, exceptions may occur during multiple calls.
The doinbackground method and the onpostexecute parameter must correspond to each other. These two parameters are specified in the generic parameter list declared by asynctask.
The parameters are accepted by doinbackground, the second is the display progress parameter, and the third is the doinbackground return parameter and the parameter passed in by onpostexecute.
There is also a small demo in the original article address, which is not described here.
The introduction in the following article is also very good.
Http://blog.csdn.net/zhenyongyuan123/article/details/5850389