Summary of the usage of the asynchronous class AsyncTask in Android

  • 2020-12-13 19:05:08
  • OfStack

This paper summarizes and analyzes the usage of asynchronous AsyncTask in Android. To share for your reference, the details are as follows:

When I was sorting out my notes recently, I found that I did not understand AsyncTask very well, so I redirected 1. I found some materials on the Internet, but I was afraid to keep them for myself

Here are two ways to explain it, each with its own emphasis:

The first explanation is:

About Async Task:

The AsyncTask feature is that tasks run outside the main thread and the callback method executes inside the main thread, effectively avoiding the hassle of using Handler

AsyncTask is an abstract class.AsyncTask defines three generic types: Params, Progress and Result.
Params starts the input parameters for task execution, such as URL requested by HTTP.
Percentage of Progress background tasks executed.
The final result of the Result background task, such as String.

The execution of AsyncTask is divided into four steps, with each step corresponding to a callback method. These methods should not be called by the application. All the developer needs to do is implement these methods.

1) Subclass Async1Task

2) Implement one or more of the following methods defined in AsyncTask

onPreExecute(), which will be called by UI thread before the actual background operation is performed. You can do some preparatory work in this method, such as displaying a progress bar on the interface.

doInBackground(Params...) Is executed immediately after the execution of the onPreExecute method, which runs in the background thread. This will be responsible for performing some of the more time-consuming back-office calculations. You can call the publishProgress method to update the real-time progress of the task. This method is abstract and must be implemented by subclasses.

onProgressUpdate(Progress...) After the publishProgress method is called, UI thread will call this method to show the progress of the task on the interface, for example through a progress bar.

onPostExecute(Result), the onPostExecute method will be called by UI thread after doInBackground completes, and the result of the calculation in the background will be passed to UI thread through this method.

To use the AsyncTask class properly, here are some guidelines you must follow:

1) Instances of Task must be created in UI thread
2) The execute method must be called in UI thread
3) Do not manually call onPreExecute(), onPostExecute(Result), doInBackground(Params...) , onProgressUpdate(Progress...) These methods
4) This task can only be executed once, otherwise an exception will occur on multiple calls
The parameters for the doInBackground method and onPostExecute must correspond, which are specified in the list of generic parameters declared by AsyncTask, the first parameter accepted by doInBackground, the second parameter to show progress, and the third parameter passed in by doInBackground return and onPostExecute.

It should be noted that AsyncTask cannot completely replace threads, and threads may be needed to implement some logic that is more complex or needs to be executed repeatedly in the background.

The second explanation:

AsyncTask abstracts the five states of background thread running, which are: 1. Ready to run, 2. Running in the background, 3. Progress update, 4.

1) Ready to run: onPreExecute(), which is called by the UI thread immediately after the task is executed. This step is usually used to set up the task and display the progress bar on the user interface (UI).

2) Running in background: doInBackground(Params...) The callback function is called by the background thread immediately after the execution of the onPreExecute() method. Time-consuming background calculations are typically performed here. The computed result must be returned by this function and passed to onPostExecute(). You can also use publishProgress(Progress...) within this function. To publish one or more progress units (unitsof progress). These values will be available on onProgressUpdate(Progress...) Is published to the UI thread.

3) Progress update: onProgressUpdate(Progress...) The function is run by the UI thread in publishProgress(Progress...) Method is called after the method is called. Generally used to dynamically display a progress bar.

4) Finish the background task: onPostExecute(Result), which is called after the background calculation. The result of the background calculation is passed to the 1 function as an argument.

5) Cancel task: onCancelled (), called when the cancel() method of AsyncTask is called

The AsyncTask constructor has three template parameters:

(1)Params, the parameter type passed to the background task.

(2)Progress, the type of progress unit (progressunits) in the background calculation and execution process. (What percentage of the background program has already been executed.)

(3)Result, the type of result returned by background execution.

AsyncTask does not always need to use all three types above. Identifying unused types is as simple as using the Void type

Number 3: Some differences between AsynTask and Handler:

1.AsyncTask, a lightweight asynchronous class provided by android, can directly inherit AsyncTask, implement asynchronous operation in the class, and provide the interface to feedback the degree of current asynchronous execution (UI progress update can be achieved through the interface), and finally feedback the execution result to the main thread of UI.

Advantages of use:

a. Simple, fast

b. Process controlled

Disadvantages of use:

a. This becomes complicated when you are using multiple asynchronous operations and need to make Ui changes.

2. The principle and advantages and disadvantages of the Handler asynchronous implementation

During the asynchronous implementation of Handler, four objects (Handler, Looper, Message and ES195enare involved, the asynchronous process is that the main thread starts Thread (child thread) athread (child thread) to run and generate ES198en-AES199en to get Message and pass it to Handler and aHandler to get Message in Looper one by one, and make UI changes.

Advantages of use:

a. Clear structure and clear function definition

b. Simple and clear for multiple background tasks

Disadvantages of use:

a. In a single background asynchronous processing, appears too much code, too complex structure (relativity)

I hope this article has been helpful in Android programming.


Related articles: