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: html "> http://www.bkjia.com/kf/201105/89977.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;
// Set three types of parameters: Params = Void, SS = Integer, Result = Void
Public class AsyncLoader extends AsyncTask {
Private Handler handler = null;
Public AsyncLoader (Handler h ){
This. handler = h;
}
@ Override
Protected 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;
}
@ Override
Protected void onPostExecute (Void v ){
This. handler. sendEmptyMessage (0 );
}
@ Override
Protected 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;
// Handler of the main thread
Private Handler handler = new Handler (){
Public void handleMessage (Message msg ){
TV. setText (msg. what + "");
If (msg. what <= 0 ){
PBar. setVisibility (View. INVISIBLE );
} Else {
PBar. setProgress (msg. what );
}
}
};
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TV = (TextView) findViewById (R. id. textView1 );
TV. setText ("Getting started ");
PBar = (ProgressBar) findViewById (R. id. progress_bar );
// AsyncTask.exe cute () must be called in the main thread
New asyncloader(handlercmd.exe cute (Void) null );
}
}