The use of Android development Asyntask

Source: Internet
Author: User

Today, summarize the use of Asyntask

Principle: Based on asynchronous message processing mechanism

Asynctask is an abstract class, so if we want to use it,

You will have to create a subclass to inherit it.

We can specify three generic parameters for the Asynctask class at the time of inheritance.

1.Parms

Parameters that need to be passed in when executing asyntask can be used in a background task

2.Process

When the background task executes, if you need to display the current progress on the interface, use the generic type specified here as the progress unit

3.Result

When the task finishes executing, if you need to return the result, use the generic type specified here as the return value type



One of the simplest custom asyntask can be written in the following way

Class Downloadtask extends asyntask<void,integer,boolean>{

......

}

The first generic parameter is void, which means that no incoming arguments are required for the background task when executing asyntask

The second generic parameter is integer, which indicates that the integer data is used as the progress display unit

The third generic parameter is Boolean, which means that Boolean data is used to feedback the execution result



The trick to using Asyntask is

The initialization of some interfaces in the OnPreExecute () method is invoked before the background task begins execution.

To perform specific time-consuming tasks in the Doinbackground () method,

UI action in the Onprogressupdate () method,

Perform the finishing touches of some tasks in the OnPostExecute () method.



Finally, if you want to start this task, just write the following code:

New Downloadtask (). Execute ();

The use of Android development Asyntask

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.