Android Asynchronous Task class analysis

Source: Internet
Author: User

First, why do you want to use the asynchronous task class?
The handler mode needs to create a new thread for each task, send a message to the UI thread through the handler object after the task completes, and finish updating the interface.This approach has a very fine control over the entire interface. But there are drawbacks, such as code bloat, so to simplify operations, the Android 1.5 SDK provides a tool class Asynctask (asynchronous Task Class),makes it easier to create asynchronous tasks.
II. Implementation of asynchronous task class
The asynchronous class implementation count demonstrates the process:
first in the main activity file: (The primary activity is used to parse the first interface of the program)
1. Create an object for the asynchronous class task and pass a reference to the text object for the construction of the asynchronous class so that the text box can be manipulated in the asynchronous task; 2. Invoke execute (Params) using the reference to the asynchronous class object. Params) method to perform an asynchronous task. It goes into the asynchronous task we created to execute;
Description:execute () can pass multiple parameters, and the type of argument passed is determined by the first parameter type of the Async class, if the first parameter type of the Async class is an integer type .then the parameters of execute pass must be integer, and so on;the Async class uses the Doinbackground (Integer ... params) method to receive the arguments that are passed by execute, which is used to receive multiple passes toparameters are stored in the array params[], Params[0] represents the first parameter passed, and so on;
                
The main activity code is as follows:

into the asynchronous task we created:
first introduce the creation of this asynchronous task class and the corresponding parameter description: A. When creating the Asynctask Class (asynchronous Task Class), set the data type ; (known as Android generic programming) parameter description:B. The popular argument:the type of the first parameter, which is the type of the parameter to be passed when executing the Execute () method, and the parameter type of the method Doinbackground () that receives the parameter remains consistent;the type of the second parameter is Onprogressupdate (..) Method receives the Publicprogress () method to pass the data corresponding to the type;the type of the third parameter is the type of the return value that receives the Doinbackground () method;

3. After the Execute method executes, the asynchronous task is initiated, and the OnPreExecute () method is immediately executed;
4. Implement the Doinbackground () method immediately upon completion of OnPreExecute (); Description of the method: A. The method is called by the quilt thread;B. Place time-consuming operations in the method;C. In this method, call the Publishprogress () method through an object of the asynchronous class, requiring the system to call the Onprogressupdate () method automatically; Similar to the usage of handle;d. The return value of the method, assigned to the third parameter of the class, result;
5. Because the Publishprogress () method is called above, the onprogressupdate () method is called automatically by the system's UI main thread;   A. This method is called by the main thread of the UI;
Asynchronous Task:
Asynchronous task implementation refreshes the list:
Implementation code:




Android Asynchronous Task class analysis

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.