Asynctask principle of implementation and advantages and disadvantages of application
Asynctask, which is a lightweight asynchronous class provided by Android , can inherit asynctask directly, implementing asynchronous operations in the class, and provides interface feedback on the current level of asynchronous execution ( UI Progress updates can be implemented through an interface ), and finally feedback the execution results to the UI main thread .
Advantages of Use :
Simple , fast
Process controllable
Disadvantages of using :
It becomes complex when multiple asynchronous operations are used and Ui changes are required .
Handler The principle of asynchronous implementation and its application advantages and disadvantages
In the Handler asynchronous Implementation , it involves Handler, Looper, Message,thread Four objects, the process of implementing asynchronous is the main thread boot thread (Child thread) runs and generates Message-looper gets a message and passes it to Handlerhandler to get the message in Looper one by one , and make UI changes.
Advantages of Use:
Clear structure with clear function definition
For multiple background tasks, simple, clear
Disadvantages of using:
In a single background asynchronous processing, it appears too much code, the structure is too complex (relativity)
Asynctask Introduction
Android 's asynctask is more lightweight than Handler (just lighter on the code, and actually more resource-intensive than Handler ), for simple asynchronous processing.
First make it clear that Android has Handler and Asynctask, both to not block the main thread (UI thread), and that updates to the UI can only be done in the main thread , so asynchronous processing is unavoidable.
Android To reduce this development difficulty, provides the asynctask. Asynctask is a packaged background task class, as its name implies, an asynchronous task.
Asynctask directly inherits from the Object class, where the position is android.os.AsyncTask. To work with Asynctask we provide three generic parameters and overload several methods ( at least one for overloading ).
asynctask defines three generic type Params,Progress , and Result.
The Params is the input parameter that initiates the task execution, such as the URLof the HTTP request.
Progress The percentage of background task execution.
result background performs the final return of the task, such as String.
Students who have used Asynctask know that an asynchronous load of data must be overridden by the following two methods:
Doinbackground (Params ...) Background execution, more time-consuming operations can be placed here. Note that it is not possible to manipulate the UIdirectly. This method is performed on a background thread, and it usually takes a long time to complete the task's main work. You can call publicprogress (Progress ...) during execution. To update the progress of the task.
OnPostExecute (Result) is equivalent to the way Handler handles the UI , where you can use the result processing operations that you get in doinbackground UI. This method is executed on the main thread, and the result of the task execution is returned as a parameter to this method
You also have to rewrite these three methods, if necessary, but not necessarily:
onprogressupdate (Progress ...) You can use the progress bar to increase user experience. This method executes on the main thread and is used to show the progress of the task execution.
OnPreExecute () here is the interface when the end user calls Excute , and the progress dialog can be displayed here before the task executes before calling this method.
oncancelled () the action to be made when the user calls Cancel
Using the Asynctask class, here are a few guidelines to follow:
The instance of the Task must be created in uithread ;
The execute method must be called in Uithread ;
Do not manually call OnPreExecute (), OnPostExecute (Result),doinbackground (Params ...), Onprogressupdate (Progress ...) These several methods;
The task can only be executed once, otherwise the exception will occur when multiple calls are made;
Handler Introduction
First, Handler mainly accepts the data sent by the child thread , and updates the UI with this data in conjunction with the main thread .
When the application starts,AndroidFirst, a main thread is opened,The main thread is in the management interfaceUIControl for event distribution,UpdateUIIt can only be updated in the main thread, and operations in the child threads are dangerous. This time,HandlerNeed to come out and solve this complex problem. BecauseHandlerRunning in the main thread(UIIn the thread),It can be used with child threads throughMessageobject to pass the data,This time,HandlerIt's going to take the thread of the child.(Sub-line Chengsedmessage ()Method Pass) MessageObject(It contains data),Put these messages into the main thread queue and update them with the main threadUI。
Second, the characteristics ofHandler
Handler can distribute Message objects and Runnable objects into the main thread , each Handler instance , will be bound to create his thread ,
It has two functions :
(1) Schedule a message or Runnable to execute somewhere in a main thread
(2) schedule an action to execute on a different thread
Some ways to distribute messages in Handler
Post (Runnable)
Postattime (Runnable,long)
Postdelayed (Runnablelong)
Sendemptymessage (int)
SendMessage (Message)
Sendmessageattime (Message,long)
Sendmessagedelayed (Message,long)
The post class method above allows you to arrange a Runnable object into the main thread queue ,
The sendMessage class method allows you to schedule a Message object with data to queue and wait for updates .
In summary: The data is simple to use Asynctask: The implementation of simple code, data volume and complex use of Handler+thread: compared to asynctask to better use of system resources and efficient
Comparison between Handler and Asynctask