Message, messagequeue, logoff, Handler details + instance

Source: Internet
Author: User

From: http://greenboy1.iteye.com/blog/844416

I. Several key concepts

1. messagequeue: a data structure. It is a message queue where messages are stored. Each thread can have up to 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 will not automatically create logoff.

When needed
Use the prepare function.

2. message: the message object, the object stored in the message queue. A Message Queue contains multiple messages.
Message Instance Object acquisition, usually using the static method obtain () in the message class, this method has multiple overloaded versions available for selection; its creation is not one

Is to directly create a new instance,
Instead, check whether there are available message instances from the message pool. If yes, retrieve the instances and return them. If the message pool does not contain

There are available message instances,
Creates a message object with the given parameters. When removemessages () is called, the message is deleted from the message queue and placed

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. Every 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

Messagequeue object, so, cannot
Accept message. If you need to accept this, define a logoff object (through the prepare function) by yourself, so that the thread has its own logoff object and

Messagequeue data structure.
Logoff extracts the message from messagequeue and submits it to handlemessage of handler for processing. After processing is complete, call

Message. Recycle () puts it in 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 corresponding Handler

Handlemessage () method of the object
Process it.

Ii. How to transmit messages between threads

1. The main thread sends a message to itself.

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 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 = logoff. getmainlogoff (); // 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 is the message code specified by the handler, which can be selectively received by the handler.
// The second three parameters have no significance.
// Object to be encapsulated by the fourth Parameter
Message MSG = handler. obtainmessage (, 1, "message sent by the main thread ");

Handler. sendmessage (MSG); // send a message

}
});
}

Class myhandler extends handler {

Public myhandler (low.logoff ){
Super (logoff );
}

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 );
}
}
}

2. Other threads send messages to the main thread

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 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 ){

// We can see that a thread is started to operate on message encapsulation and sending.
// In this way, the transmission of the original main thread becomes the sending of other threads. Is it simple? Haha
New mythread (). Start ();
}
});
}

Class myhandler extends handler {

Public myhandler (low.logoff ){
Super (logoff );
}

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 );
}
}

// Adds a Thread class
Class mythread extends thread {

Public void run (){
Logoff logoff = logoff. getmainlogoff (); // 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 );

// Construct the message object
// The first parameter is the message code specified by the handler, which can be selectively received by the handler.
// The second three parameters have no significance.
// Object to be encapsulated by the fourth Parameter
Message MSG = handler. obtainmessage (, 1, "messages sent by other threads ");

Handler. sendmessage (MSG); // send a message
}
}
}

3. The main thread sends a message to other threads.

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 savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Btntest = (button) This. findviewbyid (R. Id. btn_01 );
Textview = (textview) This. findviewbyid (R. Id. view_01 );


// Start the thread
New mythread (). Start ();

Btntest. setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view arg0 ){
// Handler instantiation thread
// It is instantiated when the thread is started.
Message MSG = handler. obtainmessage (, 1, "messages sent by the main thread ");
Handler. sendmessage (MSG );
}
});
}

Class myhandler extends handler {

Public myhandler (low.logoff ){
Super (logoff );
}

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 );
}
}

Class mythread extends thread {

Public void run (){
Logoff. Prepare (); // creates a logoff object for this thread to receive messages.

// Note: 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
// It does not exist. Now it can be instantiated.
// Here, logoff. mylogoff () obtains the logoff object of the thread.
Handler = new threadhandler (Looper. myloler ());

// 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 in the Thread class
Class threadhandler extends handler {

Public threadhandler (low.logoff ){
Super (logoff );
}

Public void handlemessage (Message MSG ){
// Here, the message in messagequeue in this thread is processed
// Here we will return a message to the main thread.
Handler = new myhandler (Looper. getmainlooper ());

Message msg2 = handler. obtainmessage (, 1, "subthread Received:" + (string) msg. OBJ );

Handler. sendmessage (msg2 );
}
}
}
}

4. other threads send messages to themselves

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 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 ){
// Start the thread
New mythread (). Start ();
}
});
}

Class myhandler extends handler {

Public myhandler (low.logoff ){
Super (logoff );
}

Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
Textview. settext (string) msg. OBJ );
}
}

Class mythread extends thread {

Public void run (){
Logoff. Prepare (); // create the logoff object of this thread.
// Here, logoff. mylogoff () obtains the logoff object of the thread.
Handler = new threadhandler (Looper. myloler ());
Message MSG = handler. obtainmessage (, 1, "Myself ");
Handler. sendmessage (MSG );

Logoff. Loop ();

}

// Define the message processing class in the Thread class
Class threadhandler extends handler {

Public threadhandler (low.logoff ){
Super (logoff );
}

Public void handlemessage (Message MSG ){
// Here, the message in messagequeue in this thread is processed
// Here we will return a message to the main thread.
// Add the component to determine whether the thread sends the information by itself.
If (msg. What = 1 & msg. obj. Equals ("Myself ")){

Handler = new myhandler (Looper. getmainlooper ());

Message msg2 = handler. obtainmessage (, 1, "Notice main thread: I received my own

Message ");

Handler. sendmessage (msg2 );
}

}
}
}
}

Note:
The layout file in the above four examples 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>

 

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.