Use of Android development Asynctask

Source: Internet
Author: User



Asynctask profile is a lightweight asynchronous class provided by Android

You can directly inherit Asynctask to implement asynchronous operations in a class and provide interface feedback on the current level of asynchronous execution (UI Progress updates can be implemented via interfaces)

The final feedback executes the result to the UI main thread


Personally feel that this is on a thread based on the addition of UI control so it is not a simple thread derived class

This enriches the control of the UI

And the UI control is generally on the main thread on the ID is 1 as to exactly which interfaces within the Asynctask are on the main thread

What are the asynchronous threads and the order in which those interfaces are called? Let's take a look at an example


First write a asynctask derived class

The interfaces that perform the operation are rewritten and printed out

public class Playasynctask extends asynctask<void, void, void> {@Overrideprotected void Doinbackground (void ... arg0) {System.out.println ("Doinbackground Thread ID" +thread.currentthread (). GetId ()); The function generates a Onprogressupdate callback publishprogress (); return null;} @Overrideprotected void OnPostExecute (void result) {//TODO auto-generated method Stubsuper.onpostexecute (Result); System.out.println ("OnPostExecute Thread ID" +thread.currentthread (). GetId ());} @Overrideprotected void OnPreExecute () {//TODO auto-generated method Stubsuper.onpreexecute (); System.out.println ("OnPreExecute Thread ID" +thread.currentthread (). GetId ());} @Overrideprotected void onprogressupdate (void ... values) {//TODO auto-generated method Stubsuper.onprogressupdate ( values); System.out.println ("Onprogressupdate Thread ID" +thread.currentthread (). GetId ());}}

An object is generated on the activity so that an asynchronous operation is performed on the main thread


System.out.println ("asynctask start");         Playasynctask playasynctask = new Playasynctask (); Playasynctask.execute ();

Look at the results of the output


What you can see is that only Doinbackground will execute on the async thread and the rest is on the main thread

Which means that other callback functions, except Doinbackground, can perform actions on the UI.



Use of Android development Asynctask

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.