Learning Source: Reference from Http://www.eoeandroid.com/forum.php?mod=viewthread&tid=49595&highlight=handler
First, related concepts
1.MessageQueue: Message Queuing, a data structure, where messages are stored. Each thread can have a maximum of one MessageQueue. The Looper object is typically used for MessageQueue management of the thread. When a thread is created, MessageQueue is not automatically created for it. When the main thread is created, a default Looper object is created automatically, and the creation of the Looper object automatically creates a MessageQueue. Other non-main threads do not automatically create Looper and are implemented by the PREPAR function when needed.
2.Message: The message object that holds the object in the messages queue. A message queue contains multiple message.
The acquisition of the message instance object, usually using the static method obtain () in the message class, has multiple overloaded versions to choose from, and it is not necessarily created to create a new instance directly, but to first look at the message If there is a message instance available in the pool, either directly out of the instance, or create a message object with the given parameters. When Removemessages () is called, the message is removed from the message queue (if an int argument is taken, only the corresponding message is emptied) and placed into the message pool. In addition to this, you can also get a message instance by using the Obtainmessage () method of the Handler object.
3.Looper: Is the manager of MessageQueue. Each MessageQueue can not be separated from the Looper, Looper object is created by the prepare function to achieve. Each Looper object is associated with one thread at a time. The Looper object of the current thread can be obtained by calling Looper.mylooper (), and when a Looper object is created, a MessageQueue object is created at the same time. In addition to the default looper of the main thread, other threads do not have MessageQueue objects by default, so the message cannot be accepted. If you need to accept, you define a Looper object (via the Prepare function) so that the thread has its own Looper object and MessageQueue data structure. Looper remove the message from the MessageQueue and leave it to Handler's handlemessage for processing. When processing is complete, call Message.recycle () to put it in the message pool.
4.Handler: The processor of the message, Handler is responsible for encapsulating the information that needs to be passed into a message, by invoking the Obtainmessage () of the Handler object, and passing the message to Looper, which is done by Handler The SendMessage () object is implemented. The message is then placed into the MessageQueue by Looper. When the Looper object sees that the MessageQueue contains a message, it broadcasts it. After the handler object receives the message, it calls the corresponding handler object's Handlemessage () method to process it.
Second, the use of handler
1. Handling Messages
You can create handler instances directly, overriding the Handlermessage method
New Handler () { @Override publicvoid handlemessage (Message msg) { Super. Handlemessage (msg); DoWork (); } };
You can also inherit handler, overriding the Handlermessage method
class myhandler extends handler{ public MyHandler (loope R Looper) { super (Looper); } @Override public void Handlemessage (Message msg) { super .handlemessage (msg); // textview.append ((String) msg.obj); // Child threads cannot manipulate UI LOG.D ("msg"
2. Send a message
Handler itself can not only send messages, but also by post to add an anonymous object to implement the Runnable interface to the message queue, the target receives the message can be recalled in its own thread to execute the method body of the run.
Handler common ways to send messages:
Post (Runnable)
Postattime (Runnable,long)
Postdelayed (Runnable Long)
Sendemptymessage (int what)
SendMessage (Message)
Sendmessageattime (Message,long)
Sendmessagedelayed (Message,long)
Third, communication
1. The main thread sends itself a message
Public classMainactivityextendsActivity {PrivateTextView TextView; Private inti = 0; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.text); } Public voidOnTouch (View v) {Looper Looper= Looper.getmainlooper ();//Looper of the main thread//A handler is created with the looper of the main thread, and the message sent by the handler is passed to the main thread's MessageQueueMyHandler handler =NewMyHandler (Looper); Handler.removemessages (0); Message msg= Handler.obtainmessage (1, 1, 1, "Main thread test" + i++); Handler.sendmessage (msg); } classMyHandlerextendshandler{ PublicMyHandler (Looper Looper) {Super(Looper); } @Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Textview.append ((String) msg.obj); } }}
2. Child threads sending messages to the main thread
Public classMainactivityextendsActivity {PrivateTextView TextView; Private inti = 0; PrivateMyHandler Handler; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.text); } Public voidOnTouch (View v) {MyThread mrunnable=NewMyThread (); Thread Thread=NewThread (mrunnable); Thread.Start (); } classMyHandlerextendshandler{ PublicMyHandler (Looper Looper) {Super(Looper); } @Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Textview.append ((String) msg.obj); } } classMyThreadImplementsrunnable{@Override Public voidrun () {//TODO auto-generated Method StubLooper Looper =Looper.getmainlooper (); //A handler is created with the looper of the main thread, and the message sent by the handler is passed to the main thread's MessageQueueHandler =NewMyHandler (Looper); Message Message= Handler.obtainmessage (1, "Child thread--and main thread test" + i++); Handler.sendmessage (message); } }
3. The main thread sends messages to other threads
Public classMainactivityextendsActivity {PrivateTextView TextView; Private inti = 0; PrivateHandler Handler; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.text); MyThread mrunnable=NewMyThread (); Thread Thread=NewThread (mrunnable); Thread.Start (); } Public voidOnTouch (View v) {Message message= Handler.obtainmessage (1, "main thread---child thread test" + i++); Handler.sendmessage (message); } classMyThreadImplementsrunnable{@Override Public voidrun () {//TODO auto-generated Method StubLooper.prepare ();//create looper for this threadLooper Looper =Looper.mylooper (); //creates a handler with the looper of the child thread, and the message sent by the handler is passed to the child thread's MessageQueueHandler =NewThreadhandler (Looper); Looper.loop (); //loop fetching messages from MessageQueue } } classThreadhandlerextendshandler{ PublicThreadhandler (Looper Looper) {Super(Looper); } @Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); //Textview.append ((String) msg.obj);//child threads cannot manipulate UILOG.D ("MSG", (String) msg.obj); } }}
4. Other threads send messages to themselves
Public classMainactivityextendsActivity {PrivateTextView TextView; Private inti = 0; PrivateHandler Handler; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.text); } Public voidOnTouch (View v) {MyThread mrunnable=NewMyThread (); Thread Thread=NewThread (mrunnable); Thread.Start (); } classMyThreadImplementsrunnable{@Override Public voidrun () {//TODO auto-generated Method StubLooper.prepare ();//create looper for this threadLooper Looper =Looper.mylooper (); //creates a handler with the looper of the child thread, and the message sent by the handler is passed to the child thread's MessageQueueHandler =NewThreadhandler (Looper); Message Message= Handler.obtainmessage (1, "Child thread---child thread test" + i++); Handler.sendmessage (message); //loop fetching messages from MessageQueueLooper.loop (); } } classThreadhandlerextendshandler{ PublicThreadhandler (Looper Looper) {Super(Looper); } @Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); //Textview.append ((String) msg.obj);//child threads cannot manipulate UILOG.D ("MSG", (String) msg.obj); } }}
Android message, Handler learning