Android asynctask Asynchronous processing

Source: Internet
Author: User
Tags abstract http request thread

The principle of a single-threaded model must be followed when developing Android applications: Android UI operations are not thread-safe and must be performed in the UI thread. Always remember two rules in a single-threaded model:

1. Do not block UI threads

2. Ensure that the Android UI Toolkit is accessed only in the UI thread

When a program starts for the first time, Android also initiates a corresponding main thread (main thread), which is primarily responsible for handling UI-related events, such as user key events, user contact screen events, and screen drawing events, and distributing related events to the corresponding components for processing. So the main thread is often called the UI thread.

For example, from the Internet to obtain a Web page, in a TextView its source code display, this involves the network operation of the program is generally required to open a thread to complete the network access, but after the source of the page, is not directly in the network operation thread Call Textview.settext () . Because the main UI thread member cannot be accessed directly from other threads

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

Activity.runonuithread (Runnable)

View.post (Runnable)

View.postdelayed (Runnable, long)

Hanlder

These classes or methods also make your code very complex and difficult to understand. However, this can get 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

It makes it easier to create long-running tasks that need to interact with the user interface without the need for threading and handler.

Asynctask is an abstract class that defines three generic types: Params,progress,result

Params the input parameters that start the task execution, such as the URL of the HTTP request.

Progress the percentage of background task execution.

Result background The results returned by the task, such as String.

The execution of Asynctask is divided into four steps, each of which corresponds to a callback method that should not be invoked by the application (that is, the user cannot call directly, but should be called by the system), and what the developer needs to do is implement these methods.

1) Sub-class of Asynctask

2 Implement the following one or several methods defined in Asynctask

(a) OnPreExecute (), which is invoked by the UI thread before performing the actual background operation. You can do some preparation work in this method, such as displaying a progress bar on the interface.

(b) Doinbackground (Params ...) is executed immediately after the OnPreExecute method is executed, which runs in a background thread. This will be the main responsibility for performing the time-consuming background computing work. You can call the Publishprogress method to update the real-time task progress. This method is an abstract method that must be implemented by subclasses.

(c) onprogressupdate (Progress ...), after the Publishprogress method is invoked, UI thread calls this method to show the progress of the task in the interface, for example, through a progress bar.

(d) OnPostExecute (result), after Doinbackground execution completes, the OnPostExecute method is invoked by the UI thread, and the results of the background calculation are passed through the method to UI thread.

For the correct use of the Asynctask class, here are a few guidelines to follow:

1 The instance of the task must be created in UI thread

2 The Execute method must be called in UI thread

3 Do not manually invoke OnPreExecute (), OnPostExecute (Result), Doinbackground (Params ...), Onprogressupdate (Progress ...) These several methods

4 The task can only be executed once, otherwise the exception will occur when multiple calls are made

Related Article

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.