Detailed explanation and usage of Android asynctask

Detailed explanation and usage of Android asynctask

Introduction:

Asynctask is an encapsulated background task class, which is asynchronous task as its name suggests.

Asynctask is a lightweight asynchronous class provided by Android. It can directly inherit asynctask, implement asynchronous operations in the class, provide interface feedback on the current asynchronous execution degree (UI progress update can be realized through the interface), and finally feed back the execution results to the UI main thread

1、 If you want to customize an asynctask, you can write a class that inherits asynctask.

eg: 、

//The first parameter is the type passed in doinbackground, the second is the type of the parameter updated in doinbackground, and the third is the parameter passed out after doinbackground is completed.

Note that one method must be implemented, that is

Generally speaking, if a custom asynctask is only used for one activity (similar to an adapter), anonymous inner classes are recommended.

If it needs to be called in different classes, write the custom asynctask in a new class, and write a completed callback in the defined asynctask.

2、 Usage:

3、 Method to stop executing task:

You can refer to "asynctask method call examples and details" in this directory

4、 Must see detailed explanation:

To work with asynctask, we need to provide three generic parameters and overload several methods (at least one).

Asynctask defines three generic types: params, progress and result: params input parameters for starting task execution, such as the URL of HTTP request. Progress percentage of background task execution. Result the final result returned by the background execution task, such as string.

Students who have used asynctask know that the following two methods should be rewritten at least when loading data asynchronously:

Doinbackground (params...) is executed in the background, and more time-consuming operations can be placed here. Note that you cannot directly operate the UI here. This method is executed in the background thread and usually takes a long time to complete the main work of the task. During execution, you can call public publishprogress (Progress...) to update the progress of the task. Onpost execute (result) is equivalent to the way the handler handles the UI. In this way, the result obtained in doinbackground can be used to handle the UI. This method is executed in the main thread, and the result of task execution is returned as a parameter of this method

If necessary, you have to rewrite the following three methods, but they are not necessary:

Onprogressupdate (progress.). After the publishprogress method is called, you can use the progress bar to increase the user experience. This method is executed in the main thread and is used to display the progress of task execution.

Onpreexecute() here is the interface when the end user calls execute. When this method is called before the task is executed, the progress dialog box can be displayed here. Oncancelled() what to do when the user calls cancel

Using the asynctask class, the following guidelines must be followed:

1. The task instance must be created in the UI thread; The 2.execute method must be invoked in UI thread. 3. Do not manually call onpreexecute(), onpostexecute (result), doinbackground (params...), onprogressupdate (Progress...); 4. The task can only be executed once, otherwise an exception will occur when calling multiple times;

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
分享
二维码
< <上一篇
下一篇>>