Asynctask (asynchronous task)

Source: Internet
Author: User

1. To solve the problem that the UI component cannot be updated by a new thread, Android provides the following solutions.

1) Use handler to implement communication between threads;

2) activity. runonuithread (runnable );

3) view. Post (runnable );

4) view. postdelayed (runnable, long ).

Ii. asynctask is suitable for simple asynchronous processing and can be implemented without using threads and handler. Asynctask <Params, progress, result> is an abstract class, which is generally used for inheritance. When inheriting asynctask, you must specify the following three generic parameters.

1) Params: input parameter type for task startup

2) progress: the type of progress value for background task completion

3) result: the type of result returned after the background task execution is completed

Iii. steps for implementing asynctask:

1) create a subclass of asynctask and specify the type for the three generic parameters. If you do not need to specify a type for a generic parameter, you can specify it as void.

2) Implement asynctask as needed:

(1) doinbackground (Params...): rewrite this method to be the task to be completed by the background thread. You can call publishprogress (Progress... values) to update the task execution progress.

(2) onprogressupdate (Progress .. values): This method is triggered when the publishprogress () method is called in the doinbackground () method to update the task execution progress.

(3) onpreexecute (): This method will be called before time-consuming operations in the background are executed. This method is usually used to complete initialization preparation, such as displaying a progress bar on the interface.

(4) onpostexecute (result): After doinbackground () is completed, the system automatically calls the 0 npostexecute () method and passes the return value of the doinbackground () method to this method.

3) Call execute (Params... Params) of the asynctask subclass to start time-consuming tasks.

4. When using asynctask, follow the following rules:

1) You must create an asynctask instance in the UI thread.

2) The execute () method of asynctask must be called in the UI thread.

3) asynctask onpreexecute (), onpostexecute (result), doinbackground (Params ...), onprogressupdate (Progress .. values) method, not called by the programmer code, but by the Android system.

4) Each asynctask can be executed only once, and multiple calls will cause exceptions.

Asynctask (asynchronous task)

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.