What is handler?
Handler is primarily used by users to communicate with the thread they create, and the Andorid application maintains a message queue in the main thread, communicating between threads through a Message object. Processing message requests in Handlermessage inside handler
In fact, learning VC + + friends should be able to understand handler, similar to VC + + message Processing
What does the message object store?
A total of 4 parameters, Arg1,arg2,obj,what
Arg1 and arg2 are of type int and can be modified by member method SetData and GetData access
obj, the object type, can store any type, such as an object, an int value, or additional data in a message.
The What,int type is a user-defined message type code that can be used to determine what message
Let's take an example, this example has a TextView show progress and completion and stop content, a ProgressBar to show progress, two buttons for starting and stopping
Activity_main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.ssln.handler.MainActivity" > <TextViewAndroid:id= "@+id/tvmsg"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "TextView" /> <ProgressBarAndroid:id= "@+id/progbar"style= "? Android:attr/progressbarstylehorizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <ButtonAndroid:id= "@+id/btnstart"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Start" /> <ButtonAndroid:id= "@+id/btnstop"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Stop" /> </LinearLayout></LinearLayout>
Mainactivity.java
PackageCom.ssln.handler;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ProgressBar;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {Private Final intupdatemsg = 1;//Update message Type Private Final intcompletemsg=2;//Completion Message Private Final intstopmsg=3;//Stop Message Private Final intMAXVALUE = 100;//ProgressBar Maximum Value Private Booleanisstop=false;//whether to stop@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); FinalTextView tvmsg =(TextView) Findviewbyid (r.id.tvmsg); FinalProgressBar ProgressBar =(ProgressBar) Findviewbyid (R.id.progbar); Button btnstart=(Button) Findviewbyid (R.id.btnstart); Button Btnstop=(Button) Findviewbyid (r.id.btnstop); //set the maximum valueProgressbar.setmax (MAXVALUE); //Initializes a handler for updating TextView and ProgressBar FinalHandler Handler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseUPDATEMSG:tvMsg.setText (msg.obj.toString ()+ "%"); ProgressBar. Setprogress (Integer.valueof (msg.obj.toString ())); Break; CaseCOMPLETEMSG:tvMsg.setText ("The update operation was completed"); Break; CaseSTOPMSG:tvMsg.setText ("Stop Update operation"); Break; } } }; //Stop OperationBtnstop.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {isstop=true; } }); //start using the button to create a new thread to update the dataBtnstart.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {NewThread () { Public voidrun () {Message msg; for(inti = 0; i < MAXVALUE; i++) { if(isstop) { Break; } //loop send messages for internal communicationmsg =NewMessage (); Msg.what=updatemsg; Msg.obj= i + 1; Handler.sendmessage (msg); Try { //sleep for some timeThread.Sleep (150); } Catch(interruptedexception e) {e.printstacktrace (); } } if(!isstop) { //Send Completion messagemsg =NewMessage (); Msg.what=completemsg; Handler.sendmessage (msg); } Else { //Send Stop messagemsg =NewMessage (); Msg.what=stopmsg; Handler.sendmessage (msg); Isstop=false; }}}.start (); } }); }}
When the Start button is clicked, a updatemsg is sent for each interval of 150 milliseconds, where obj is the current progress value, and the progress bar setting progress to Obj,textview displays the percentage
When the progress reaches 100%, a completemsg completion message is sent for the TextView update display
If you click Stop during the update process, the starting thread will jump out of the loop and send a stopmsg Stop message
Android Internal Communication Handler