Android Thread Asynctask

Source: Internet
Author: User

In the previous blog for everyone to share about the Android multithreading, presumably we have a clear understanding of why the use of multi-threaded Android, I am in a simple two sentences, Android rules UI interface update must be in the main thread, The time-consuming operation to access the network must be performed in a child thread, so if you want to become a real Android developer, multithreading is a required course for you. This article gives you a simple thread class Asynctask that Android has packaged for us.

Asynctask, a lightweight asynchronous class provided by Android, can directly inherit Asynctask, implement asynchronous operations in a class, and provide interface feedback about 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. For more information on Asynctask please view: http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html

Asynctask in Android for us encapsulated four methods, specific introduction please see:

  

See here you probably have a certain understanding of asynctask, below I will take a progress bar effect to guide you to learn more about it.

Our layout file Activity_main.xml does not have too many controls, just a horizontal progress bar:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"Android:paddingbottom="@dimen/activity_vertical_margin"Android:paddingleft="@dimen/activity_horizontal_margin"Android:paddingright="@dimen/activity_horizontal_margin"Android:paddingtop="@dimen/activity_vertical_margin"Tools:context=". Mainactivity"> <ProgressBar Android:id="@+id/progressbar1"style="? android:attr/progressbarstylehorizontal"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:layout_centerhorizontal="true"android:layout_centervertical="true"/></relativelayout>

Our main activity:

 Public classMainactivity extends Activity {PrivateProgressBar Mprogressbar; PrivateMyasynctask Mtast; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mprogressbar=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); Mtast=NewMyasynctask ();//Create our custom AsynctaskMtast.execute (); }    //prevents the thread from waiting when we reopen@Overrideprotected voidOnPause () {super.onpause (); if(Mtast! =NULL&& mtast.getstatus () = =AsyncTask.Status.RUNNING) {            //The Cancel () method simply marks the corresponding asynctask as the cancel state, not the actual execution of the cancellation thread. Mtast.cancel (true); }    }        classMyasynctask extends Asynctask<void, Integer, void>{        //The Doinbackground () method is the only method in the four methods in Asynctask that is not executed by the main thread@Overrideprotectedvoid Doinbackground (void ... arg0) { for(intI=0; i< -; i++){                if(IsCancelled ()) {//determine the status of cancel                     Break;                } publishprogress (i); Try{Thread.Sleep ( -);//Analog Network loading}Catch(interruptedexception e) {e.printstacktrace (); }            }            return NULL; } @Overrideprotected voidonprogressupdate (Integer ... values) {super.onprogressupdate (values); if(IsCancelled ()) {//determine the status of cancel                return; } mprogressbar.setprogress (values[0]); }            }    }

There is no difficulty in the code, the key parts have been commented for everyone, if you have questions, please leave a message to discuss.

  

Android Thread 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.