Comparison of advantages and disadvantages of Asynctask and handler

Source: Internet
Author: User

Asynctask implementation of the principle and the advantages and disadvantages of application

Asynctask, a lightweight asynchronous class provided by Android, can directly inherit Asynctask, implement asynchronous operations in a class, and provide interface feedback about the current level of asynchronous execution (UI Progress updates can be implemented via interfaces). The final feedback executes the result to the UI main thread.

Advantages of Use:

Simple, fast

Process controllable

Disadvantages of using :

It becomes complex when multiple asynchronous operations are used and UI changes are required.

Handler the principle of asynchronous implementation and its application advantages and disadvantages

When Handler is implemented asynchronously, it involves Handler, Looper, Message,thread four objects, The process of implementing asynchrony is that the main thread initiates the thread (child thread) to run and generates Message-looper to get a message and pass it to Handlerhandler to get the message in Looper, and make UI changes.

Advantages of Use:

Clear structure with clear function definition

For multiple background tasks, simple, clear

Disadvantages of using:

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

Asynctask Introduction  Android's Asynctask is more lightweight than handler (just lighter on the code, and actually more resource-intensive than handler), for simple asynchronous processing. The first thing to make clear is that Android has handler and Asynctask, both to not block the main thread (the UI thread), and that the UI update can only be done in the main thread, so asynchronous processing is unavoidable.

Android to reduce this development difficulty, provides the asynctask. Asynctask is a packaged background task class, as its name implies, an asynchronous task.

Asynctask directly inherits from the object class, where the position is android.os.AsyncTask. To work with Asynctask we are going to provide three generic parameters and overload several methods (at least one for overloading).

Asynctask defines three types of generic type params,progress and result.

    • The Params is the input parameter that initiates the task execution, such as the URL of the HTTP request.
    • Progress the percentage of background task execution.
    • Result background performs the final return of the task, such as String.

Students who have used Asynctask know that an asynchronous load of data must be overridden by the following two methods:

    • Doinbackground (Params ...) is performed in the background, and more time-consuming operations can be placed here. Note that it is not possible to manipulate the UI directly. This method is performed on a background thread, and it usually takes a long time to complete the task's main work. You can call Publicprogress (Progress ...) during execution. To update the progress of the task.
    • OnPostExecute (Result) is equivalent to the way handler handles the UI, where it is possible to use the resulting processing action UI in Doinbackground. This method is executed on the main thread, and the result of the task execution is returned as a parameter to this method

You also have to rewrite these three methods, if necessary, but not necessarily:

    • Onprogressupdate (Progress ...) You can use the progress bar to increase user experience. This method executes on the main thread and is used to show the progress of the task execution.
    • OnPreExecute () Here is the interface when the end user calls Excute, and the progress dialog can be displayed here before the task executes before calling this method.
    • Oncancelled () The action to be made when the user calls Cancel

Using the Asynctask class, here are a few guidelines to follow:

    • The instance of the task must be created in the UI thread;
    • The Execute method must be called in the UI thread;
    • Do not manually call OnPreExecute (), OnPostExecute (Result), Doinbackground (Params ...), Onprogressupdate (Progress ...) These several methods;
    • The task can only be executed once, otherwise the exception will occur when multiple calls are made;
Handler IntroductionFirst, handler mainly accepts the data sent by the child thread, and updates the UI with this data in conjunction with the main thread. When the application starts, Android first turns on a main thread, the main thread is the UI control in the admin interface, the event is distributed, the update UI can only be updated in the main thread, and the operations in the child threads are dangerous. This time, handler need to come out to solve this complex problem. Since handler runs in the mainline approached (UI thread), it and the child thread can pass the data through the Message object, at this time, the handler undertakes to accept the child thread passed (the child thread passes with the Sedmessage () method) the Message object ( Contains data), put these messages into the main thread queue, with the main thread to update the UI. Handler characteristics handler can distribute the Message object and the Runnable object into the main thread, each handler instance is bound to create his threads, it has two functions: (1) Schedule messages or runnable to execute somewhere in a main thread (2) Arranging an action to perform some methods of distributing messages in handler in different threads post (Runnable) postattime (Runnable,long) postdelayed (Runnable long) Sendemptymessage (int) sendMessage (Message) sendmessageattime (Message,long) sendmessagedelayed (Message,long) The above Post class method allows you to arrange a runnable object into the main thread queue, SendMessage class method, which allows you to schedule a message object with data to queue and wait for updates.

Comparison of advantages and disadvantages of Asynctask and handler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.