Message, messagequeue, logoff, Handler details + instances in Android

Source: Internet
Author: User
Message, messagequeue, logoff, and handler in Android + Example 1. Several key concepts

1. messagequeue: a data structure. It is a message queue where messages are stored. Each thread can have at most one messagequeue data structure.
Messagequeue is not automatically created when a thread is created. A logoff object is usually used to manage the messagequeue of the thread. When the main thread is created,
And the creation of the logoff object will automatically create a message queue. Other non-main threads do not automatically create logoff.
Use the prepare function.

2. message: the message object, the object stored in the message queue. A Message Queue contains multiple messages.
Obtain the object of the message instance, usually using the static method obtain () in the message class. This method has multiple overloaded versions to choose from; it does not necessarily create a new instance directly,
Instead, check whether there are available message instances from the message pool. If yes, retrieve the instances and return them. If no message instance is available in the message pool,
Creates a message object with the given parameters. When removemessages () is called, the message is deleted from the message queue and put into the message pool. Except for the above
You can also get a message instance through the obtainmessage () of the handler object.

3. Logoff:
Is the manager of messagequeue. Each messagequeue cannot be separated from the logoff object. The creation of the logoff object is implemented through the prepare function. At the same time, each logoff object
Associated with a thread. You can call lorule. mylorule () to obtain the lorule object of the current thread.
Create a messagequeue object when creating a logoff object. Except for the default Logoff of the main thread, other threads do not have messagequeue objects by default. Therefore
Accept message. If you need to accept it, define a looper object (through the prepare function) by yourself, so that the thread has its own Looper object and messagequeue data structure.
Logoff extracts the message from messagequeue and submits it to handlemessage of handler for processing. After the processing is complete, call message. Recycle () to put it into the message pool.

4. Handler:
The handler of the message handler is responsible for encapsulating the information to be passed into a message and implementing it by calling the obtainmessage () of the handler object;
Send the message to logoff, which is implemented by sendmessage () of the handler object. Then, logoff puts the message into messagequeue.
When the logoff object sees that messagequeue contains a message, it is broadcasted. After the handler object receives the message, it calls the handlemessage () method of the corresponding handler object.
Process it.
Ii. How to transmit messages between threads 1. The main thread sends messagepackage test to itself. message; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. view. view; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private button btntest; private textview; private handler; @ override Pu BLIC void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btntest = (button) This. findviewbyid (R. id. btn_01); textview = (textview) This. findviewbyid (R. id. view_01); btntest. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view arg0) {logoff = logoff. getmainlogoff (); // The logoff object of the main thread // handler is created with the logoff object of the main thread., // Therefore, the message sent by the handler will be passed to the messagequeue of the main thread. Handler = new myhandler (lofter); handler. removemessages (0); // construct the message object // The first parameter: the specified message code, it makes no sense for Handler to selectively receive // The second three parameters // The fourth parameter needs to encapsulate the object message MSG = handler. obtainmessage (, 1, "message sent by the main thread"); handler. sendmessage (MSG); // send message});} class myhandler extends handler {public myhandler (Looper loler) {super (loid);} public void handlemessage (Message MSG) {super. handlemessage (MSG); textview. se Ttext ("I am the handler of the main thread and received the message:" + (string) MSG. OBJ) ;}}2. Other threads send messagepackage test to the main thread. message; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. view. view; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private button btntest; private textview; Private handler; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btntest = (button) This. findviewbyid (R. id. btn_01); textview = (textview) This. findviewbyid (R. id. view_01); btntest. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view arg0) {// it can be seen that a thread is started to operate on message encapsulation and sending. // this way, the original main thread sends messages. It becomes the sending of other threads. Is it easy? Haha new mythread (). start () ;}});} class myhandler extends handler {public myhandler (Looper loler) {super (loid);} public void handlemessage (Message MSG) {super. handlemessage (MSG); textview. settext ("I am the handler of the main thread and received the message:" + (string) MSG. OBJ) ;}} // added a Thread class mythread extends thread {public void run () {Looper lods = Looper. getmainlooper (); // The loler object of the main thread // handler is created for the loler object of the main thread. // Therefore, this hand The message sent by ler will be passed to the messagequeue of the main thread. Handler = new myhandler (lofter); // construct the message object // The first parameter: the message code specified by the user, it makes no sense for Handler to selectively receive // The second three parameters // The fourth parameter needs to encapsulate the object message MSG = handler. obtainmessage (, 1, "other threads have sent messages"); handler. sendmessage (MSG); // send message }}3. The main thread sends messagepackage test to other threads. message; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. view. View; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private button btntest; private textview; private handler; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btntest = (button) This. findviewbyid (R. id. btn_01); textview = (textview) This. findviewbyi D (R. id. view_01); // start the thread new mythread (). start (); btntest. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view arg0) {// here, the message MSG = Handler has been instantiated when the handler is instantiated during thread startup. obtainmessage (, 1, "messages sent by the main thread"); handler. sendmessage (MSG) ;}});} class myhandler extends handler {public myhandler (Looper loler) {super (lods);} public void handlemessage (Message MSG) {supe R. handlemessage (MSG); textview. settext ("I am the handler of the main thread and received the message:" + (string) MSG. OBJ) ;}} class mythread extends thread {public void run () {loid. prepare (); // create the logoff object of the thread to receive messages. // note: the handler is defined in the main thread, // The Handler object is directly used. Are you looking for it? Where is it instantiated? // You can see it now ??? Haha, it cannot be instantiated at the beginning, because the logoff object of this thread does not exist yet. Now it can be instantiated. // here logoff. mylohand() obtains the loler object of the thread. Handler = new threadhandler (lohand. mylogoff (); // is there any doubt about this method? // It is actually a loop in which messages are retrieved from messagequeue cyclically. // Do you know that you have new messages ??? Logoff. loop () ;}// define the message processing class threadhandler extends handler {public threadhandler (Looper loler) {super (lods);} public void handlemessage (Message MSG) in the Thread class) {// here, the message in messagequeue in this thread is processed. // here we return a message to the main thread, Handler = new myhandler (loler. getmainlooper (); message msg2 = handler. obtainmessage (, 1, "subthread Received:" + (string) MSG. OBJ); handler. sendmessage (msg2) ;}}4. Other threads send their own message package test. Message; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. view. view; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private button btntest; private textview; private handler; @ override public void oncreate (bundle savedinstancestat E) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btntest = (button) This. findviewbyid (R. id. btn_01); textview = (textview) This. findviewbyid (R. id. view_01); btntest. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view arg0) {// start the thread new mythread (). start () ;}}) ;}class myhandler extends handler {public myhandler (Looper loler) {super (Looper) ;} pub LIC void handlemessage (Message MSG) {super. handlemessage (MSG); textview. settext (string) MSG. OBJ) ;}} class mythread extends thread {public void run () {loid. prepare (); // create the logoff object of this thread // here logoff. mylohand() obtains the loler object of the thread. Handler = new threadhandler (lohand. mylofter (); message MSG = handler. obtainmessage (, 1, "Myself"); handler. sendmessage (MSG); logoff. loop ();} // defines the message processing class threadhandl In the Thread class. Er extends handler {public threadhandler (Looper loler) {super (loid);} public void handlemessage (Message MSG) {// here, the message in messagequeue in this thread is processed. // here, we return a message to the main thread. // Add it to the thread to check whether the message is sent by the thread itself. If (MSG. what = 1 & MSG. OBJ. equals ("Myself") {handler = new myhandler (logoff. getmainlooper (); message msg2 = handler. obtainmessage (, 1, "Notification main thread: I received the message that I sent myself"); handler. sendmessage (msg2) ;}}}note: the layout of the above four examples The local file is the same file main. xml <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: rientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: id = "@ + ID/view_01" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <button Android: id = "@ + ID/btn_01" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Test message"/> </linearlayout>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.