Asynchronous tasks in Android-----------Asynctask use

Source: Internet
Author: User

    1. Problems------Why if the red Asynctask

      1. Sends an HTTP request to the server, the server returns some data to the user, and then displays the data in the UI

        This time there is a phenomenon: because the Android access server must be placed in the sub-thread, and the UI update data must be placed in the main thread, so when the main thread to show the data, encountered some time-consuming operations such as access to the server, database queries and other operations, the main thread will be blocked, The user will see the ANR (the program is not responding). And Asynctask is a good solution to this kind of problem.

    2. How to use Asynctask

      1. Create a class mytask inherit from Asynctask

      2. Implementing the Asynctask Doinbackground () method

This method must be implemented primarily to write asynchronous task main code to perform some time-consuming operations that can call Publishprogress () to update progress information during execution

Onprogressupdate ()

When the Publishprogress () method is called, the progress information is presented to the UI

OnPreExecute () The method is called first when the Execute () method is executed, primarily to make some declarations about some of the UI's Information OnPostExecute () The method is called when the operation of the asynchronous task ends, and is used to update the UI information Execute () method, This method is used to open the execution of an asynchronous task and must be opened in the main thread.

This article is from the "Five Piglets" blog, please be sure to keep this source

Asynchronous tasks in Android-----------Asynctask use

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: 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.