Android Asynctask Usage Note introduction and precautions

Source: Internet
Author: User

Speaking of Android Asynctask, you have to talk about the single-threaded model in Android, only allow the UI thread to operate the interface in Android, Workingthread is not allowed to manipulate the UI. The time-consuming operation should be kept as far as possible in Workingthread, preventing the UI thread from updating the interface, causing the ANR (application not responding) exception. This ensures the stability and smoothness of the UI operation and avoids the clutter of the UI caused by multi-threading operations.

We can use the thread pool to operate, and Android provides us with a handler mechanism that actually gives us a asynctask component. When we use the thread pool to operate, we need to manage threads, and we need to use handler to send messages, but too much code in the process is too complicated.

asynctask Parameter Construction

Asynctask is an abstract class that encapsulates three generic parameters. Typically, we need to integrate this class to implement functionality.

    • The Params is the input parameter that initiates the task execution, such as the URL of the HTTP request.
    • Progress the percentage of background task execution.
    • Result background performs the final return of the task, such as String.

Asynctask sub-Class callback method

    • Doinbackground (Params ...) : A method that must be overridden by a background task.
    • OnPostExecute (Result) is equivalent to how handler handles the UI, which is automatically invoked when the Doinbackground is complete.

The following methods are not required to be replicated

    • Onprogressupdate (Progress ...) The method of updating the execution progress of the task. Publishprogress ()
    • OnPreExecute () Perform a background time-consuming task before calling!
    • Oncancelled () The action to be made when the user calls Cancel

Android Asynctask Usage Note introduction and precautions

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.