Android message mechanism (1)

Source: Internet
Author: User

Turn: http://jbg168.blog.163.com/blog/static/99683620107301828231/

 

Android has a message queue statement, which can be understood as follows: if a tunnel is a message queue, every car in it is a message queue, here, we will ignore the overtaking and other factors, but the advanced tunneling cars will come out first. This mechanism is the same as our android message mechanism.
I. Role description
1. loue :( it is equivalent to a tunnel) a thread can generate a loue object that manages the Message Queue (fleet, Message tunnel) in this thread ).
2. Handler: You can construct a Handler object to communicate with logoff, so as to push new messages to the Message Queue, or receive messages sent by logoff (retrieve from Message Queue.
3. Message Queue: used to store messages placed by threads.
4. thread: the UI thread is usually the main thread, and Android will create a Message Queue for it when starting the program.
Each thread can contain a loose object and a MessageQueue data structure. In your application, you can define the sub-category of Handler to receive messages sent by logoff.

In your Android program, when a new Thread is created or executed, the Message Loop is not automatically created.
Android does not have the Global Message Queue data structure. For example, objects in different APK cannot exchange messages through the Massage Queue ).
For example, the Handler object of thread A can transmit messages to other threads so that other threads B or C can send messages to thread A (stored in the Message Queue of thread ).
Only the objects to which thread A belongs can process the messages in the Message Queue of thread.
You can use logoff. mylogoff to obtain the logoff object of the current thread.
MHandler = new EevntHandler (Looper. myloler () can be used to construct the Handler object of the current thread. EevntHandler is the sub-class of the Handler implemented by itself.
Use mHandler = new EevntHandler (Looper. getMainLooper (); the Handler object that can be generated to process the main thread; among them, EevntHandler is the sub-class of the Handler implemented by itself.

The description may be too similar. The following are some examples:
Ii. Example
1. message transmission between different components in the same thread
The logoff class is used to manage Message Exchange between objects in a specific thread ). Your application can generate many threads. A thread can have many components, which often need to exchange messages with each other. If you need this, you can construct a logoff object for the thread to manage message exchange. The logoff object creates a MessageQueue data structure to store messages (including UI events or System events) sent from each object ). For example:
Each thread can contain a loose object and a MessageQueue data structure. In your application, you can define the sub-category of Handler to receive messages sent by logoff.
Message transmission between different components in the same thread:
Public class Activity1 extends Activity implements OnClickListener {
Button button = null;
TextView text = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity1 );
Button = (button) findviewbyid (R. Id. BTN );
Button. setonclicklistener (this );
TEXT = (textview) findviewbyid (R. Id. content );
}
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. BTN:
Loginlogoff = logint. mylogint (); // obtain the logint in the current thread
Myhandler mhandler = new myhandler (loler); // construct a handler to allow communication with Looper
// Components such as buton can be sent to logoff by mhandler, placed in messagequeue, and mhandler can also accept messages from logoff.
Mhandler. removemessages (0 );
String msgstr = "communication between different components in the main thread: the message comes from the button ";
Message M = mhandler. obtainmessage (1, 1, 1, msgstr); // construct the message to be passed
Mhandler. sendmessage (m); // send a message: the system automatically calls the handlemessage method to process the message.
Break;
}
}
Private class myhandler extends handler {
Public myhandler (low.logoff ){
Super (logoff );
}
@ Override
Public void handlemessage (Message MSG) {// process the message
Text. settext (msg. obj. tostring ());
}
}
}

Note:
When this program is started, the current thread (that is, the main thread) has generated a loose object and a messagequeue data structure.
Lorule = lorule. mylorule ();
Call the static mylogoff () function of the logoff class to obtain the logoff object in the current thread.
Mhandler = new myhandler (logoff );
Construct a myhandler object to communicate with logoff. Activity and other objects can send messages to logoff through the myhandler object, and then put them into messagequeue. The myhandler object also plays the role of listener and can receive messages sent by the logoff object.
Message M = mhandler. obtainmessage (1, 1, 1, OBJ );
Construct a message object and store the data in the object.
Mhandler. sendmessage (m );
The mhandler object is used to send the message m to logoff and put it in messagequeue.
At this point, when the logoff object sees a message m in messagequeue, it will be broadcast out. When the mhandler object receives this message, it will call its handlemessage () function for processing, the output is "This my message! "On the screen,

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.