Each context has a looper handle the messages sent by each handler, and the function that Looper processes the message is:
Msg.target.dispatchMessage (msg);
Here msg is handler sent to Looper message, MSG bound handler, so there will not be a message wrong handler situation, in the handler Dispatchmessge Source:
/*** Handle system messages here. */ Public voidDispatchMessage (Message msg) {if(Msg.callback! =NULL) {handlecallback (msg); } Else { if(Mcallback! =NULL) { if(Mcallback.handlemessage (msg)) {return; }} handlemessage (msg); } }
Here is the processing of messages, subdivided into three kinds:
1. First message callback processing messages, if empty continue
2. On the callback interface that is handler processing the message, if NULL continues
3. Finally, the message is processed by the handler Handlemessage (..) Method.
The following program mainly verifies these three ways:
Public classMainactivityextendsappcompatactivity {PrivateButton Btn_send,btn_start,btn_begin; PrivateMyHandler Handler; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btn_send=(Button) Findviewbyid (r.id.btn_send); Btn_start=(Button) Findviewbyid (R.id.btn_start); Btn_begin=(Button) Findviewbyid (R.id.btn_begin); Func (); Btn_send.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {handler.sendemptymessagedelayed (10000, 1000); Handler.sendemptymessagedelayed (10001, 2000); Handler.sendemptymessagedelayed (10002, 4000); } }); Btn_start.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {MyThread (); } }); Btn_begin.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {/** Send messages in a child thread*/ NewThread (NewRunnable () {@Override Public voidrun () {Message msg=Message.obtain (); Msg.what= 10000; /*note here that in the child thread initialization handler must pass in the main line thread looper, otherwise it will go wrong, * This is because the looper to get the message is in the main thread*/Handler=NewMyHandler (mainactivity. This. Getmainlooper ()); Handler.sendmessage (msg); }}). Start (); } }); } /*(1) to schedule messages and * Runnables to being executed as some point in the future;*/ Private voidfunc () {/*when defining handler, the callback interface has been implemented, overriding Handlermessage (..) method to process messages*/Handler=NewMyHandler (NewHandler.callback () {@Override//rewrite the handlemessage (..) in the interface Method Public Booleanhandlemessage (Message msg) {Switch(msg.what) { Case10000: System.out.println ("Execute handler Interface callback: 10000"); Break; Case10001: System.out.println ("Execute Handler Interface callback: 10001"); Break; Case10002: System.out.println ("Execute Handler Interface callback: 10002"); Break; default: Break; } //If True is returned here, execution of the handler override will also be performed .//The void Handlemessage (Message msg) method. If you return ture, do not execute return true; } }); } /** to enqueue an action to being performed on a different thread than your own.*/ Private voidMyThread () {Thread T=NewThread (NewRunnable () {@Override Public voidrun () {/*The callback is specified when the message is initialized, which is actually a child thread, and if a message callback is specified * then this will be performed first instead of executing the callback function in handler .*/Message msg= Message.obtain (Handler,NewRunnable () {@Override Public voidRun () {//Specifies the callback for the message Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Callback to execute Message"); } }); Msg.what= 10000; Handler.sendmessagedelayed (MSG,3000); } }); T.start (); } Private classMyHandlerextendshandler{/** Rewrite Handlemessage (..) in the handler class method to process messages **/@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case10000: System.out.println ("Execute Handlemessage Method overridden in handler: 10000"); Break; Case10001: System.out.println ("Execute Handlemessage Method overridden in handler: 10001"); Break; Case10002: System.out.println ("Execute Handlemessage Method overridden in handler: 10002"); Break; default: Break; } } PublicMyHandler (Callback Callback) {Super(callback); } PublicMyHandler (Looper Looper) {Super(Looper); } }}
XML file
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.skymaster.hs.review.MainActivity" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send"android:textsize= "30SP"Android:id= "@+id/btn_send"android:layout_centervertical= "true"Android:layout_centerhorizontal= "true"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Start"android:textsize= "30SP"Android:id= "@+id/btn_start"Android:layout_below= "@id/btn_send"android:layout_centervertical= "true"Android:layout_centerhorizontal= "true"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Begin"android:textsize= "30SP"Android:id= "@+id/btn_begin"Android:layout_below= "@id/btn_start"android:layout_centervertical= "true"Android:layout_centerhorizontal= "true"/></relativelayout>
View Code
Message mechanism in the discussion of Android