Asynctask of Android Source code parsing

Source: Internet
Author: User

First, let's take a look at Asynctask's definition form:

 1publicabstractclass Asynctask<params, Progress, result>}

First Asynctask It is an abstract class, consisting of three generic types, with the following meanings:

    • Params: It represents the type of the request parameter
    • Progress: The type of progress performed on a task
    • Result: Returns the type of the result

However, the above three parameters must not necessarily be set to void when not required, and no return type.

Then we look at its execution process, including the following methods:

    • Execute (params ... params), which is called when we perform an asynchronous operation, indicating that the task is beginning to execute.

    • protected void OnPreExecute () {}, after invoking the Execute method, the method is executed, it executes in the UI thread, used to initialize some UI space, etc.

    • Protected abstract Result Doinbackground (params): The method is executed after the execution of the OnPreExecute, it executes in the background, and accepts an array parameter of type Params, is used to request a network, and it returns the result of a type. The method can update the request progress while executing the network request, calling Publishprogress (Progress ... values).

    • protected void Onprogressupdate (Progress ... values), if the Publishprogress method is called in the Doinbackground method, then the method will be executed, It is performed on the UI thread, constantly changing the progress according to values, to achieve the desired effect.

    • protected void OnPostExecute (result result), which is executed after the Doinbackground method has been executed, and can be followed by subsequent UI operations based on the results returned by the Doinbackground. This shows that it is working in the UI thread.

Asynctask of Android Source code parsing

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.