1. Layout: Main. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:max="100" android:maxHeight="5dp" android:id="@+id/probar" android:layout_width="300dp" android:layout_height="wrap_content" /> <Button android:id="@+id/net" android:onClick="onClick" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/net" /> </LinearLayout>
2. A mainactivity. Java button and a progress bar
Public class mainactivity extends activity {private textview text; private button netbtn; private progressbar probar; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); text = (textview) This. findviewbyid (R. id. text); netbtn = (button) This. findviewbyid (r.id.net); probar = (progressbar) This. findviewbyid (R. id. probar);} public void onclick (view v) {Switch (v. GETID () {Case r.id.net: progressasynctask task = new progressasynctask (text, probar); // call the background thread task.exe cute (1000); break ;}}}
3. asynctask. Java background Thread class
// When the execute () method is called in the UI thread, onpreexecute () is executed first, doinbackground () is followed, and onpostexecute () is finally executed. // Asynctask generic. The three parameter types can be arbitrary // The first parameter is the parameter type of the doinbackground () method // The second parameter is onprogressupdate () method parameter type // The third parameter is the doinbackground () method return type and onpostexecute () parameter type public class progressasynctask extends asynctask <integer, integer, string> {private textview text; private progressbar probar; Public progressasynctask (textview text, progressbar probar) {super (); this. TEXT = text; this. probar = probar;} // run in the background thread // variable length parameter, Params is a number Group. @ Overrideprotected string doinbackground (integer... params) {netdemo net = new netdemo (); int I = 0; for (I = 10; I <= 100; I ++ = 10) {net. operate (); // publishprogress () calls back the onprogressupdate () method. Publishprogress (I);} return I + Params [0]. intvalue () + "";} // run @ overrideprotected void onpreexecute () {text in the UI thread. settext ("START Asynchronous Operation download");} // run in the UI thread // result is the return value of the doinbackground () method @ overrideprotected void onpostexecute (string result) {text. settext ("Download ended" + result);} // run @ overrideprotected void onprogressupdate (integer... values) {probar. setprogress (Values [0]) ;}}
4. netdemo. Java imitates Network Operations
public class NetDemo {public void operate(){try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
5. Result: