Asynchronous task Asynctask for Android

Source: Internet
Author: User

Today the author learns the asynchronous task of Android, the asynchronous task sounds a good tall thing, in fact, it is not very tall.

What exactly is an asynchronous task? Asynchronous tasks are widely spoken as functional requirements that are done in a multi-threaded manner, relative to the synchronization

, an asynchronous task is the first step that can be completed by opening a thread to complete the second step without waiting for the second step to complete

To do the third step of the thing. And the synchronous task is to complete the task in order, the first step is completed, the second step is completed before the start

The third step of things. The wirelessly asynchronous task is worth Asynctask this class. This class is actually a package for Thread+handler.

We can use this class to simply complete asynchronous tasks, like filling in the blanks without having to design the process ourselves. and the Asynctask package

The thread pool, which is more efficient than using thread directly.

Having said so much, let's talk about the use of asynctask.

Asynctask is easy to use, first create a Asynctask object, and then call its execute (Params) method. Paste the code below and step through the analysis.

NewAsynctask<string, Integer, string>(){            protected voidOnPreExecute () {System.out.println ("OnPreExecute");                        }; @Overrideprotectedstring Doinbackground (String ... params) {System.out.println ("Doinbackground ..." +params[0]); Publishprogress (1); return"This is the result set returned";                        }; protected voidOnPostExecute (String result) {System.out.println ("OnPostExecute ..." +result);                        }; protected voidonprogressupdate (integer[] values) {System.out.println ("Onprogressupdate ..." +values[0]);                                }; }.execute ("This is the parameter");

From the above code we can see that creating a Asynctask object requires passing in three generics.

Asynctask<params, Progress, result>

The Params is the input parameter that initiates the task execution, such as the URL of the HTTP request.

The first generic is used to restrict the type of arguments passed in by the Execute (params) and Doinbackground (params) methods, and the parameters passed in the Execute method are used in the Doinbackground callback method.

Progress the percentage of background task execution.

The second generic is used to restrict the types of parameters passed in by the Onprogressupdate (Progress) and Publishprogress (Progress) methods. The parameters passed in the Publishprogress method are used in the Onprogressupdate callback method.

Result background performs the final return of the task, such as String.

The third generic is the type used to limit the return value of result Doinbackground and the OnPostExecute (result) method passed in the parameter, and the result returned in Doinbackground is used in the OnPostExecute callback method

Here is the result of the execution:

08-06 12:28:33.680:i/system.out (1367): OnPreExecute08-06 12:28:33.710:i/system.out (1367): Doinbackground ..... This is the parameter 08-06 12:28:33.720:i/system.out (1367): Onprogressupdate ... 108-06 12:28:33.720:i/system.out (1367): OnPostExecute ..... This is the result set returned

The following is affixed to the function and usage of each method of the Asynctask class:

Execute (params ... params) starts a task and begins the execution process of the task

void OnPreExecute () is executed in Uithread before the start of the sub-threading work, and is typically used to display a prompt view

Result Doinbackground (params ... params) executed in workerthread, which usually takes a long time to complete the task's main work

void OnPostExecute (result result) is executed in Uithread after Doinbackground () is executed, and is typically used to update the interface

Publishprogress (Progress ... values): In a sub-thread, publish the current progress

void Onprogressupdate (Progress ... values): Update progress in the main thread

To open an asynchronous task, and of course to cancel an asynchronous task, the method of canceling an asynchronous task is as follows:

The call to the Cancle (Boolean) method cancels the asynchronous task that is also executing, and the callback IsCancelled () method returns True after the method is called.

After the method is called, oncancelled (object) replaces the OnPostExecute (object) callback after the execution of Doinbackground (object[]). To ensure that the current task is canceled as soon as possible, you should always check the iscancelled () return value condition ....

The general flow of asynchronous tasks is as follows:

Asynchronous task Asynctask for Android

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.