When we use asynctask in Android, it is generally used as the internal class of the activity. In this way, you can simply update the progress onprogressupdate and onpostexecute when the execution is complete, and directly operate the interface element control of the activity. However, if we want to simplify our code and clarify the function division, we 'd better not use internal classes. At this time, we can use handler to implement this requirement.
For the usage of asynctask, see my blog: http://www.cnblogs.com/ghj1976/archive/2011/05/06/2039204.html.
The above is to use asynctask as the internal class of the activity.
The following is an example. asynctask sub-classes and asynctask sub-classes are parallel.
First, the layout file: Main. xml
There are two controls, one being a progress bar and the other being a text box, which will be updated at the update progress:
xml version="1.0" encoding="utf-8"?>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent">ProgressBar android:id="@+id/progress_bar"android:layout_width="200dip" android:layout_height="200dip"android:layout_gravity="center" android:max="100" android:progress="0">ProgressBar>TextView android:text="TextView" android:id="@+id/textView1"android:layout_width="wrap_content" android:layout_height="wrap_content">TextView>LinearLayout>
Asynctask subclass: asyncloader. Pay attention to the constructor here, which is one of the key points here.
Package ghj1976.asynctask; import Android. OS. asynctask; import Android. OS. Handler; // you can specify Params = void, progress = integer, and result = voidpublic class asyncloader extends asynctask.
{Private handler = NULL; Public asyncloader (handler h) {This. handler = H ;}@ overrideprotected void doinbackground (void... params) {publishprogress (10); try {thread. sleep (2000);} catch (interruptedexception e) {e. printstacktrace ();} publishprogress (50); try {thread. sleep (3000);} catch (interruptedexception e) {e. printstacktrace ();} publishprogress (100); return NULL;} @ overrideprotected void onpostexecute (void v) {This. handler. sendemptymessage (0) ;}@ overrideprotected void onprogressupdate (integer... values) {This. handler. sendemptymessage (Values [0]) ;}}
Asynctaskactivity code:
Package ghj1976.asynctask; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. widget. progressbar; import android. widget. textview; public class asynctaskactivity extends activity {public progressbar pbar; private textview TV; // handlerprivate handler = new handler () {public void handlemessage (Message MSG) {TV. settext (MSG. what + ""); If (MSG. what else {pbar. setprogress (MSG. what) ;}};@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); TV = (textview) findviewbyid (R. id. textview1); TV. settext ("Preparation"); pbar = (progressbar) findviewbyid (R. id. progress_bar); // asynctask.exe cute () to call new asyncloader(handlercmd.exe cute (void) null) in the main thread );}}