Android hander and message

Source: Internet
Author: User

I have talk about handler in an article before. http://blog.csdn.net/elfylin/article/details/6085042.

Here mainly I want to discuss something between hander and message.

1. Message has some frequently used members as below:

Int what; Message ID

Int arg1;

Int arg2;

Object OBJ; // an arbitrary object to send to the recipient, useful when use messenger to send message.

Bundle data;

Handler target;

Int flags;

Long when;

Runnable callback;

Messenger replyto;

2 message. sendtotarget ()

Message has a function, sendtotarget

    public void sendToTarget() {        target.sendMessage(this);    }

Which will call it's target's sendmessage,

And then push the message to the target's queue.

And final call the target'sHandlemessage (),
(It's target is a handler .)

That is to say, Handler's handlemessage.

3. Message. Obtain (Message MSG );

Newmessage = message. Obtain (oldmessage)

Genereate a new message

The newmessage and the old one share the same parameters as below:

What, arg1, arg2, replyto, Data, target, callback.

4. Message obtain (handler H)

Get a new message,
Message's target is H, and others are default (unset ).

5. Message'sOtherObtain Function

Message obtain (handler H, int what, object OBJ)

Obtain (handler H, int what, int arg1, int arg2)

... And so on

All in all what is pass in the parameter list, what will be the value of the new message.

6. Handle. obtainmessage ()

    public final Message obtainMessage()    {        return Message.obtain(this);    }

It pass it seft to message. Obtain, and get a default message with the target of the handler itself.

7. Handler's other obtainmessage ()

Message obtainmessage (INT what)

Message obtainmessage (INT what, object OBJ)

Message. obtainmessage (INT what, int arg1, int arg2)

Message obtainmessage (INT what, int arg1, int arg2, object OBJ)

The same as handler. obtainmessage (). But what is passed into the parameter list, what will be the new value of the new message.

8. Handler's sendmessage Function

Send the message to it's message queue, and it's handlermessage (MSG), will final called with the MSG passed in as parameter.

9. Extra

Lorule. Prepare
A. New messagequeue
Message Queue to logoff. mqueue
Currentthread to logoff. mthread
Logoff to sthreadlocal.
B. mylooper, get sthreadlocal's Looper

Who decides which thread a handler's code works?
The messagequeue, handler send.
A. How to define a handler's messagequeue?
Handler (lofter) This case pass one in.
Hanlder (), and other format without the loadepassed in,
It's the messagequeue of current thread's logoff.
It is created in the thread that runs it constructor function. (does it also belong to the thread ?)
B. Who decides the messagequeue 'working thread?
The thread who run the messagequeue, that code run the logoff's loop.

Question?
What wowould happen, if we define a handler's queue in one thread (that is creates it in one thread)
And run it message loop in another thread?
I havn't tried, I will try someday. Maybe there will be some exception.

A complex example of using message passing through a lot of classes.

1. browseractivity. oncreatecontextmenu.

final HashMap<String, WebView> hrefMap =                        new HashMap<String, WebView>();                hrefMap.put("webview", webView);final Message msg = mHandler.obtainMessage(                        FOCUS_NODE_HREF, id, 0, hrefMap);webView.requestFocusNodeHref(msg);

Mhandler is a handler type member of browseractivity.

That's the message's target is mhandler.

The message's obj is hrefmap.

(Here we get message1 name hrefmsg, And it's target is browseractivity. mhandler)

2. webview. requestfocusnodehref (MSG );

mWebViewCore.sendMessage(EventHub.REQUEST_CURSOR_HREF,                contentX, contentY, hrefMsg);

Mwebviewcore is a object of webviewcore

(Still message1)

3. webviewcore. sendmessage.

       void sendMessage(int what, int arg1, int arg2, Object obj) {        mEventHub.sendMessage(Message.obtain(null, what, arg1, arg2, obj));    }

Here are two steps:

A get a new message with it'sOBJAsMessage1 (hrefmesg), and also set the what and arg1, arg2
Value.

(Here we got Message 2 and it's target is null, with what, arg1 and arg2 set)

Meventhub is an object of eventhub

4. eventhub. sendmessage

        private synchronized void sendMessage(Message msg) {            if (mBlockMessages) {                return;            }            if (mMessages != null) {                mMessages.add(msg);            } else {                mHandler.sendMessage(msg);            }        }

Mhandler is a object of hander belonging to webviewcore

So mhandler's handlermessage will be called with MSG as parameter.

5. webviewcore. handler. handlemessage

                        case REQUEST_CURSOR_HREF: {                            Message hrefMsg = (Message) msg.obj;                             HashMap<String, WebView> hrefMap= (HashMap<String, WebView>)hrefMsg.obj;                            hrefMsg.getData().putString("url",                                    nativeRetrieveHref(msg.arg1, msg.arg2));                            hrefMsg.getData().putString("title",                                    nativeRetrieveAnchorText(msg.arg1, msg.arg2));                            hrefMsg.getData().putString("src",                                    nativeRetrieveImageSource(msg.arg1, msg.arg2));                            hrefMsg.sendToTarget();                            break;                        }

Here we also do two things

A. Get message1 out of message2.

B. Call message1's send target to it's target.

So it's send to browseractivity's mhandler. And browseractivity. mhandler. handlemessage ()

Will be called.

So we final came back.

We change from message1 to message 2, and final back to message1.

But what's the meaning, does it make any sense?

The most import steps are:

1. As we genereate message2, we add new Arg to it, but we doesn't change message1.

2. As we transfer message2 back to message1, we put some extra to it, which couldn't be get in browseractivity.

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.