I recently reviewed my notes and found that AsyncTask was not very easy to understand. I had to look for some information on the Internet, so I did not dare to share it with you here.
Here there are two ways to explain, each with a focus:
The first explanation:
Introduction to Async tasks:
AsyncTask is characterized by running tasks outside the main thread, and the callback method is executed in the main thread, which effectively avoids the trouble of using Handler.
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 background tasks.
Result: The Result returned when the task is executed in the background, such as String.
The execution of AsyncTask is divided into four steps. Each step corresponds to a callback method, which should not be called by the application. All developers need to do is implement these methods.
1) subclass Async1Task
2) implement one or more of the following methods defined in AsyncTask
OnPreExecute (), this method will be called by the UI thread before the actual background operation is executed. You can make some preparations in this method, such as displaying a progress bar on the interface.
DoInBackground (Params...) will be executed immediately after the onPreExecute method is executed. This method runs in the background thread. Here, we will primarily perform those time-consuming background computing tasks. You can call publishProgress to update the real-time task progress. This method is an abstract method and must be implemented by sub-classes.
OnProgressUpdate (Progress...), after the publishProgress method is called, UI thread will call this method to display the Progress of the task on the interface, for example, display through a Progress bar.
OnPostExecute (Result). After doInBackground is executed, the onPostExecute method will be called by the UI thread, and the background computing Result will be passed to the UI thread through this method.
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 the onPreExecute (), onPostExecute (Result), doInBackground (Params...), onProgressUpdate (Progress...) methods.
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 first parameter is the parameter accepted by doInBackground, and the second parameter is the parameter indicating the progress, the third is the doInBackground return and the parameters passed in by onPostExecute.
It should be noted that AsyncTask cannot completely replace the thread. In some logic that is complicated or needs to be executed repeatedly in the background, it may need to be implemented by the thread.
The second explanation:
AsyncTask abstracts the five statuses of background thread running, namely: 1. Prepare for running; 2. running in the background; 3. Updating progress; 4. Completing background tasks, 5. Cancel the task. For these five phases, AsyncTask provides five callback functions:
1) Prepare to run: onPreExecute (). The callback function is called by the UI thread immediately after the task is executed. This step is usually used to create a task and display the progress bar on the user interface (UI.
2) running in the background: doInBackground (Params...). The callback function is called immediately after the onPreExecute () method is executed by the background thread. Generally, time-consuming background computing is executed here. The calculation result must be returned by this function and passed to onPostExecute. You can also use publishProgress (Progress...) in this function to publish one or more progress units (unitsof Progress ). These values will be published to the UI thread in onProgressUpdate (Progress.
3) Progress Update: onProgressUpdate (Progress...), which is called by the UI thread after the publishProgress (Progress...) method is called. It is generally used to dynamically display a progress bar.
4) Complete the background task: onPostExecute (Result), called after the background computing is complete. The result of background computing is passed as a parameter to this function.
5) cancel the task: onCancelled (), called when the cancel () method of AsyncTask is called
The AsyncTask constructor has three template parameters:
(1) Params: parameter type passed to the background task.
(2) Progress: indicates the type of the progress unit during background computing execution. (That is, the background program has executed a few percent .)
(3) Result: the type of the Result returned by the background execution.
AsyncTask does not always need to use all three types above. It is easy to identify a type that is not used. You only need to use the Void type.
Third: some differences between AsynTask and Handler:
1. asyncTask is a lightweight asynchronous class provided by android. It can inherit AsyncTask directly and implement asynchronous operations in the class, the interface is provided to feedback the degree of asynchronous execution (UI progress update can be implemented through the interface), and the execution result is finally fed back to the main UI thread.
Advantages:
A. simple and fast
B. Controllable Process
Disadvantages:
A. It becomes complicated when multiple asynchronous operations and Ui changes are required.
2. Principles and advantages and disadvantages of Handler asynchronous implementation
When Handler is implemented asynchronously, it involves four objects: Handler, logoff, Message, and Thread. the asynchronous process is that the main Thread starts thread (subthread) à Thread (subthread) run and generate Message-à logoff to get the Message and pass it to Handler and à Handler to get the Message in logoff one by one, and change the UI.
Advantages: www.2cto.com
A. Clear structure and clear Function Definition
B. simple and clear for multiple background tasks
Disadvantages:
A. in asynchronous processing of a single backend, there are too many codes and the structure is too complex (relative)
Author: tianfeng701