Detailed explanation of how Android asynctask implements asynchronous task processing

Detailed explanation of how Android asynctask implements asynchronous task processing

When developing Android applications, we must abide by the principle of single thread model: Android UI operations are not thread safe, and these operations must be executed in UI threads.

Detailed explanation of Android single thread model concept: https://www.oudahe.com/p/25541/

In the single threaded model, always remember two rules:

Do not block UI threads

Ensure that the Android UI toolkit is accessed only in the UI thread

When a program is started for the first time, Android will start a corresponding main thread at the same time. The main thread is mainly responsible for handling UI related events, such as user key events, user touch screen events and screen drawing events, and distributing relevant events to corresponding components for processing. Therefore, the main thread is often called UI thread.

For example, getting a web page from the Internet and displaying its source code in a TextView, which involves a network operation usually requires a thread to complete the network access, but after obtaining the page source code, it is not possible to use TextView.setText () directly in the network operation thread, because other threads can not directly access the main UI thread members.

Android provides several ways to access UI threads in other threads.

These classes or methods will also make your code very complex and difficult to understand. However, this gets worse when you need to implement some very complex operations and need to update the UI frequently.

To solve this problem, Android 1.5 provides a tool class: asynctask, which makes it easier to create long-running tasks that need to interact with the user interface. It can be implemented without the help of threads and handlers.

Asynctask is an abstract class. Asynctask defines three generic types params, progress and result.

◆ params input parameters for starting task execution, such as URL of HTTP request.

◆ progress percentage of background tasks executed.

◆ result the final result returned by the background execution task, such as string.

The execution of asynctask is divided into four steps. Each step corresponds to a callback method. These methods should not be called by the application. What developers need to do is to implement these methods.

Subclassing asynctask implements one or more of the following methods defined in asynctask

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

Doinbackground (params...) will be executed immediately after the onpreexecute method is executed, which runs in the background thread. It will be mainly responsible for the time-consuming background computing. You can call the publishprogress method to update the real-time task progress. This method is an abstract method, and subclasses must implement it.

Onprogressupdate (Progress...), after the publishprogress method is called, the UI thread will call this method to display the progress of the task on the interface, for example, through a progress bar.

Onpostexecute (result). After doinbackground execution is completed, onpostexecute method will be called by UI thread, and the background calculation results will be passed to UI thread through this method

In order to correctly use the asynctask class, the following guidelines must be followed:

1) An instance of the task must be created in the UI thread

2) the execute method must be invoked in UI thread.

3) Do not manually call onpreexecute(), onpostexecute (result), doinbackground (params...), onprogressupdate (Progress...) methods

4) The task can only be executed once, otherwise an exception will occur when it is called multiple times

Get a web page from the Internet and display its source code in a textview

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>