[Transfer]android learning----message mechanism

Source: Internet
Author: User

I. Description of the role

1. Looper : A thread can produce a Looper object that manages this line thread message queue.

2. Handler : You can construct a Handler object to communicate with Looper in order to push a new message into the message queue, or to receive a message from the Looper (removed from message queue).

3. Message queue: Used to hold messages placed by the thread.

4. thread : The UI thread is usually the main thread, and the Android launcher will create a message Queue for it.

Each thread can contain a Looper object and a MessageQueue data structure. In your application, you can define handler subcategories to receive messages sent by Looper.

In your Android program, when a new thread is created or executed (thread), it does not automatically establish its message Loop.

There is no global (global) Message queue data structure in Android, for example, objects in different apk cannot exchange messages through the massage queue.

Message in thread A's message queue, only the object that the thread a belongs to can handle.

Use Looper.mylooper to get the Looper object for the current thread.

Use Mhandler = new Eevnthandler (Looper.mylooper ()), a handler object that can be used to construct the current thread, where Eevnthandler is a subcategory of the handler that is self-implemented.

Use Mhandler = new Eevnthandler (Looper.getmainlooper ()), a handler object that can be used to process the main thread, where Eevnthandler is a subcategory of the handler that is self-implemented.

This may be too much to describe, here are a few practical examples to illustrate:

Ii. examples

1. Message passing between different components within the same thread

The Looper class is used to manage the exchange of messages (message exchange) between objects within a particular thread. Your application can generate a number of threads. While a thread can have many components, these components often need to exchange messages with each other. If this is necessary, you can construct a Looper object for the thread to manage the message exchange. The Looper object creates a MessageQueue data structure to hold messages from each object (including UI events or system events). Such as:

Each thread can contain a Looper object and a MessageQueue data structure. In your application, you can define handler subcategories to receive messages sent by Looper.

Message passing between different components of the same thread:

Public class Activity1 extends Activity implements onclicklistener{

Buttonbutton =null;

Textviewtext =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:

Looper Looper = Looper. Mylooper (); //Get the current line thread Looper

MyHandler Mhandler =new MyHandler (Looper); //construct a handler so that it can communicate with Looper

components such as//Buton can be sent to Looper by Mhandler, then put into MessageQueue, and Mhandler can also accept messages from Looper

Mhandler.removemessages (0);

String msgStr = "Main thread different component communication: Message from Button";

Message m = mhandler.obtainmessage (1, 1, 1, MSGSTR); //construct the message to be delivered

Mhandler.sendmessage (m); //Send message: The system automatically calls the Handlemessage method to process the message

break;

}

}

Private class MyHandlerextends handler{

Public MyHandler (Looper Looper) {

Super (Looper);

}

@Override

Public void handlemessage (message msg) { //processing message

Text.settext (Msg.obj.toString ());

}

}

}

Description

When this program starts, the current thread (that is, the main thread) has created a Looper object and has a MessageQueue data structure.

1. Looper = Looper.mylooper ();

Call the static Mylooper () function of the Looper category to obtain the Looper object for the current line thread.

2. Mhandler = new MyHandler (looper);

Constructs a MyHandler object to communicate with Looper. Objects such as activity can pass messages to Looper by MyHandler objects, and then into MessageQueue, MyHandler objects also play the role of listener to receive messages sent by Looper objects.

3. Message m = mhandler.obtainmessage (1, 1, 1, obj);

A Message object is constructed and the data is stored in the object.

4. Mhandler.sendmessage (m);

It passes the message m to Looper through the Mhandler object and then into the MessageQueue.

At this point, the Looper object sees the message m in the MessageQueue, broadcasts it out, Mhandler the object receives this message, calls its handlemessage () function to process, and outputs "This my message!" On the screen,

Role Overview (Review):

(1) The UI thread is usually the main thread, and the Android launcher will create a MessageQueue for it.

(2) Of course, a Looper object is needed to manage the MessageQueue.

(3) We can construct a handler object to push a new message into the message queue, or receive a message from Looper (taken out of the message queue).

(4) Thread A's handler object can be passed to another thread, allowing other threads such as B or C to send messages to thread A (in the message queue of a).

(5) The message in thread A's messages queue, only the object to which thread a belongs can be processed.

2. Child threads passing messages to the main thread

Public class Activity2extends Activityimplements onclicklistener{

Buttonbutton =null;

Textviewtext =null;

Myhandlermhandler =null;

Threadthread;

@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:

thread =new MyThread ();

Thread.Start ();

break;

}

}

Private class MyHandlerextends handler{

Public MyHandler (Looper Looper) {

Super (Looper);

}

@Override

Public void handlemessage (message msg) {//Processing message

Text.settext (Msg.obj.toString ());

}

}

Private class MyThreadextends thread{

@Override

Public void Run () {

Looper curlooper = Looper. Mylooper ();

Looper mainlooper = Looper. Getmainlooper ();

String msg;

if (curlooper==null) {

Mhandler =new MyHandler (Mainlooper);

msg = "Curlooper is null";

}Else{

Mhandler =new MyHandler (Curlooper);

msg = "This is Curlooper";

}

Mhandler.removemessages (0);

Message m =mhandler.obtainmessage (1, 1, 1, msg);

Mhandler.sendmessage (m);

}

}

}

Description

Android will automatically create a message Queue for the main thread. In this strand thread does not establish a message Queue. Therefore, the Mylooper value is null, and mainlooper points to the looper of the mainline thread. Then, execute to:

Mhandler = new MyHandler (mainlooper);

This mhandler belongs to the main thread.

Mhandler.sendmessage (m);

The M message is deposited into the message queue of the main thread. Mainlooper sees a message in the message queue and processes it, and the main thread executes to Mhandler's handlemessage () to process the message.

The next section will write an example of a multi-threaded request for network data.

[Transfer]android learning----message mechanism

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.