Handler
1. Message
Messsge is a message passed between threads, which can carry a small amount of information inside, to exchange data between different threading, the What field of the message, and in addition to using the Arg1 and Arg2 fields to carry some integral data, using the Obj field to carry a single Object object.
2.Handler
Handler the processor, it is used primarily to send and process messages. Sending a message typically uses the handler SendMessage method, and the sending message passes through a series of passes and ends up in the handler Handlemessage () method.
3.MessageQueue
MessageQueue is the meaning of Message Queuing, which is primarily used to store all messages sent through handler. This part of the message will remain in the message queue and wait for it to be processed. there will be only one MessageQueue object in each thread.
4.Looper
Looper is the steward of the MessageQueue in every thread, and after calling the Looper loop () method, it goes into a wireless loop, and whenever a message is found in MessageQueue, it is removed, And passed to Handler's Handlemessage () method. There will only be one Looper object in each thread.
Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Privatebutton button; PrivateHandler Handler; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewButtonlistener ()); Handler=NewFirsthandler (); } Public classButtonlistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {//when the user taps the button, we create a message object and send the object with handler. Message msg =Handler.obtainmessage (); Msg.what= 2; Handler.sendmessage (msg); //The preceding line of code places the Message object in the message queue//1.Looper will take the message object out of the message queue. //2.Looper will find the handler object corresponding to the Message object. //3.Looper will invoke the Handlemessage (message msg) method of the Handler object to process the message object } } Public classFirsthandlerextendsHandler {@Override Public voidhandlemessage (Message msg) {
int what = Msg.what; LOG.D ("Mainactivity", "msg:" +what ); } }}
Mainthread sending data to Workerthread
Public classMainactivityextendsActivity {Privatebutton button; PrivateHandler Handler; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {LOG.D ("Mainactivity", "Thread------->" +Thread.CurrentThread (). GetName ()); Message msg=Handler.obtainmessage (); Msg.what= 100; Handler.sendmessage (msg); } }); Thread T=NewNetworkthread (); T.start (); } Public classNetworkthreadextendsThread {@Override Public voidrun () {//Preparing Looper ObjectsLooper.prepare (); //generate a Handler object in WorkthreadHandler =NewHandler () { Public voidhandlemessage (Message msg) {LOG.D ("Mainactivity", "Thread------->" +Thread.CurrentThread (). GetName ()); intwhat =Msg.what; LOG.D ("Mainactivity", "What:" +what ); } }; //after calling the Looper loop () method, the Looper object will constantly take the message object from the message queue and call handler's Handlemessage () method to process the message object. //If there are no objects in the message queue, the thread is blocked. Looper.loop (); } } }
Asynchronous message processing mechanism--handler usage