Android Asynctask Learning

Source: Internet
Author: User

The Android program has UI processes and background processes, and when you do some time-consuming operations, the user experience is affected by the likelihood of suspended animation in the UI process, so those time-consuming processes tend to be in the background process and the user experience is better. Network situation is not stable, in the request data to the server, may be used to the background process (Android4.0 later forced to no longer access the network in the main thread), may also use Asynctask, the following will introduce the difference between Asynctask and handler, for a comparison.

Asynctask is an abstract class that is a background task class that executes asynchronously.

Public abstract class Asynctask<params, Progress, result>

Asynctask inherits from object, runs in the background, and results are published on the UI process.

An asynchronous task is defined as three generics:

Params: An input parameter that initiates task execution, such as a URL

Progress: Percentage of background task execution

Result: The final result of a background execution task

Four steps to perform:

OnPreExecute (): Executes before the Doinbackground method, which displays the dialog box display Progress (ProgressDialog).

Doinbackground (Params ...) : Time-consuming, network-access tasks in the background can be called publicpregress (Progress ... values) to update task progress.

Onprogressupdate (Progress ...) : The main thread executes, showing the progress of the task execution, can increase the progress bar to increase the user experience degree.

OnPostExecute (Result): The UI processing method in which the return value obtained in Doinbackground will be used as a parameter to the method.

Oncancelles (): Action to be taken when the user calls Cancel, after cancel () and Doinbackground ()

Asynchronously loading data overrides at least: Doinbackground () and OnPostExecute ()

Note : The task is created in the UI process and is started in the UI process using the Execute () method call, and the task can only be executed once, otherwise an exception is thrown.

You can call Cancel to try to cancel the task execution.

Where the internal class status flags the current task status: finished (end), PENDING (not yet started), runing (executing), you can call GetStatus () to view the current task status.

Comparison of Asynctask and handler:

Asynctask: Process controllable, simple and fast; multiple asynchronous operations and UI changes are not available

Handler: The structure is clear, the function definition is clear, multiple tasks background execution is simple and clear; only one background task is too complex to be busy.

Handle: Involves the handler,looper,message,thread of four objects. The process of implementing async is that the main thread starts the thread child thread, and the child thread runs the build Message-looper. Gets the message and passes it to Handler,handler to get the message in Looper one by one and make the UI change.

Android Asynctask Learning

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.