Android asynchronous task analysis, android asynchronous task

Source: Internet
Author: User

Android asynchronous task analysis, android asynchronous task
I. Why use the asynchronous task class?
In Handler mode, a new thread needs to be created for each task. After the task is completed, a message is sent to the UI thread through the Handler object to update the interface. This method controls the entire interface very precisely. However, there are also some disadvantages, such as code bloated, so to simplify the operation, Android 1.5 sdk provides a tool class AsyncTask (asynchronous task class), making it easier to create asynchronous tasks.

II. Implementation of asynchronous task classes
Asynchronous class implementation counting demo process:
First, in the main Activity file: (the main Activity is used to parse the first interface of the Program)

1. Create an asynchronous task object and pass a Text object reference to the asynchronous class structure so that the text box can be operated in the asynchronous task;2. Call the execute (Params .. Params) method to execute an asynchronous task by referencing an asynchronous class object. That is, it enters the created asynchronous task for execution;
Note: execute () can pass multiple parameters. The passed parameter type is determined by the first parameter type of the asynchronous class. If the first parameter type of the asynchronous class is Integer, the parameters passed by execute must be Integer, and so on. In asynchronous classes, doInBackground (Integer... params) method to receive parameters passed by execute. this parameter is written in the format of representation, which is used to receive multiple passed parameters and is saved in the array params, params [0] indicates the first parameter passed, and so on;

The main Activity code is as follows:

Enter the created asynchronous task:
First, we will introduce the creation of this asynchronous task class and the corresponding parameter descriptions:. when creating an AsyncTask class (asynchronous task class), you must set the data type. (Android generic programming) parameter description: B. to put it simply, the type of the first parameter is the type of the parameter to be passed when the execute () method is executed, and the method doInBackground () to receive the parameter () the parameter types are consistent. The second parameter type is onProgressUpdate (..) the type of the data that the method receives from the publicProgress () method. The type of the third parameter is the type that the doInBackground () method returns;

3. After the execute method is executed, the asynchronous task is started, and the onPreExecute () method is executed immediately;
4. After onPreExecute () is executed, immediately execute the doInBackground () method;Description of this method:. this method is called by the quilt thread; B. put time-consuming operations in this method; c. in this method, the publishProgress () method is called through an asynchronous object, and the system must automatically call the onProgressUpdate () method; similar to handle usage; d. the return value of this method is assigned to the third parameter Result of the class;
5. Because publishProgress () is called above, the system UI main thread is required to automatically call the onProgressUpdate () method;Description of this method:. this method is called by the main UI thread; B. when you call the publishProgress () method, you can pass the parameter to onProgressUpdate (Integer... values); values [] array to receive passed parameters, used to display the results on the UI control;
6. When the background operation ends, the onPostExecute () method will be automatically called, and the Result of the third parameter of the class will be passed to this method. You can display the Result in
UI control;
The asynchronous task class code is as follows:

Asynchronous task:




Refresh the asynchronous task implementation list:

Implementation Code:


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.