A program in Android runs called a process, with at least one main thread in the process. The main thread is used to handle user touch operations and to distribute touch action events to the response control. If the time consuming operation is performed, the download, the disk reads the file, does not run in the main thread operation, only on the worker thread. The main thread sends messages and data to the worker thread, and the worker threads are always fed back to the thread. Thread Communication: Multiple threads send and receive messages and data to each other. Android rules only the main thread can modify the UI interface and controls (except that the progress bar can be modified on the worker thread), and if the worker thread wants to modify the UI, it can only notify the main thread to modify it. Handler: Handle, handle, remote control, Operation board.
Mainactivity:
Packagecom.sxt.day06_01;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.os.SystemClock;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.ProgressBar;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Handler mhandler; Private Static Final intdownloading=1;//in the download, the inner class is used so with static type, Private Static Final intdownload_finish=10;//Download CompleteProgressBar Mprogressbar; TextView mtvprogress; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); Setlistener (); Inithandler (); } Private voidInithandler () {//main thread Processing messageMhandler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseDOWNLOADING:mtvProgress.setText (Msg.arg1+"%"); Break; CaseDOWNLOAD_FINISH:Toast.makeText (mainactivity. This, "Download Done", 2000). Show (); Break; } } }; } Private voidSetlistener () {Findviewbyid (r.id.btnstartdownload). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {NewThread () {//Create thread (worker thread) Public voidrun () { for(inti=1;i<=100;i++) {Systemclock.sleep (20); Mprogressbar.setprogress (i);//This control can be modified in the worker thread//no change. Sends a message, sent 100 times, sent to the message queue,Message Msg=message.obtain ();//The static method gets the message, takes an object from the thread pool, is a buffer domain of memory, if the first time no message object will be new, will not be destroyed after use, the next time directly from the pool, the thread pool to put a maximum of 10,msg.what=downloading;//What property is the type of message, integral type. Msg.arg1=i;//Arg1,arg2 is also an integral type, and an Obj property is an object type. Mhandler.sendmessage (msg);//send message to main thread, display percent } //send an end message after the loop is finishedMessage msg=Message.obtain (); Msg.what=Download_finish; Mhandler.sendmessage (msg); }; }.start (); } }); } Private voidInitview () {Mprogressbar=(ProgressBar) Findviewbyid (R.ID.PB); Mtvprogress=(TextView) Findviewbyid (r.id.tvprogress); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
Page:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <ButtonAndroid:id= "@+id/btnstartdownload"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start Download" /> <ProgressBarprogress bar Android:id= "@+id/pb"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"style= "@android: Style/widget.progressbar.horizontal"Android:max= "+"/> <TextViewpercentage of digital Android:id= "@+id/tvprogress"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "20SP"Android:text= "0%"/></LinearLayout>
Android 36 Thread Communication