Android asynctask details

Asynctask vs. handler

1) The principle of asynctask implementation, and the applicable advantages and disadvantages

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

Advantages of using:

Disadvantages of using:

2) The principle of asynchronous implementation of handler and its applicable advantages and disadvantages

The asynchronous implementation of handler involves four objects: handler, looper, message and thread. The process of realizing asynchronous is that the main thread starts thread (sub thread) and thread (sub thread) to run and generate message - long looper to obtain messages and pass them to handler. Long handler obtains messages in looper one by one and makes UI changes.

Advantages of using:

Disadvantages of using:

Asynctask introduction

Android's asynctask is lighter than handler and is suitable for simple asynchronous processing.

First of all, it is clear that Android has handlers and asynctasks in order not to block the main thread (UI thread), and UI updates can only be completed in the main thread, so asynchronous processing is inevitable.

In order to reduce the difficulty of this development, Android provides asynctask. Asynctask is an encapsulated background task class, which is asynchronous task as its name suggests.

Asynctask directly inherits from the object class and its location is Android os. AsyncTask。 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.

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

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

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

A super simple example of understanding asynctask:

main. xml

MainActivity. java

NetOperator. java

ProgressBarAsyncTask . java

Original link: http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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