Android Message processing (handler related)

Source: Internet
Author: User

First, the message class does not need to say much, the internal use what property to hold the identifier, with obj (object) to hold the data to carry, with target (target) to hold the target handler.

So there needs to be a handler (handle) class to handle the message, by calling the SendMessage (message) method in the handler class, I've always thought that the logic of the method name is very strange, and now I know it because it knows another class:

MessageQueue (Message Queuing), after calling SendMessage, handler calls two internal methods that are not overloaded but similar to overloads, and finally, like this

Sent = Queue.enqueuemessage (msg, uptimemillis);

Send a message (send) to an internally created MessageQueue object, and set the target property in the message to itself.

At this point another class, Looper (Circulator), is also mentioned. When the program runs, Android will create a looper in the mainline thread,

1     /**2 * Initialize The current thread as a looper, marking it as an3 * application ' s main looper. The main looper for your application4 * is created by the Android environment, so you should never need5 * To-call the This function yourself. See also: {@link#prepare ()}6      */7      Public Static voidPreparemainlooper () {8Preparefalse);9         synchronized(Looper.class) {Ten             if(Smainlooper! =NULL) { One                 Throw NewIllegalStateException ("The main Looper has already been prepared."); A             } -Smainlooper =Mylooper (); -         } the}
system Calls

It keeps trying to take a message out of the queue in the form of a for-dead loop.

Message msg = Queue.next ();

Takes the handler DispatchMessage (Message) method that is stored in the target that is called in this Message. Like this:

Msg.target.dispatchMessage (msg);

Handler uses this method to make a slight judgment, and then forwards to his own handlemessage (message) method to process the message, which, like OnCreate, is rewritten by the programmer and called by the system.

The rest is very simple, the Handlemessage method is written by the programmer himself, want to do something.

It's usually a switch based on what's in the message and it's OK to react differently to different sources.

so summarize :

Materializing on the programmer to do two things.

The first is to create handler, rewrite the good method, decide to use it to do something;

The latter is divided into two cases.

If you need to carry information when calling handler, use the Message.obtain () method or the New keyword to get the message object, and then use the SendMessage (message) method of the handler object for handler processing.

If you do not need to carry extra information, only need to notify handler to do something, you do not need to get the message object, directly using the handler object's Sendemptymessage (int) method, handler will be like this

Message msg == what;

Create a Message object yourself and use the INT value passed in in the method as the what property of the message.

The first step is to define handler.

The second step is to send the message.

Such

Ps:

If you want to use handler outside the main thread, pay attention to one problem.

First, according to the above, the message was taken out and distributed by Looper from the queue, just on its own thread. However, the looper used in the main thread is created by the Android system, and the user does not automatically create a new looper when creating the thread, so Looper tells you

Final Looper me = mylooper (); if NULL {     thrownew runtimeexception ("No Looper; Looper.prepare () wasn ' t called on the This thread. " );}

This, handler told you.

Mlooper = looper.mylooper (); if NULL {    thrownew  runtimeexception (        "Can ' t create handler inside Thread that have not called looper.prepare () ");}

This one.

Therefore, to create a handler in the new line thread need to call the Looper.prepare () method to prepare a looper, this method automatically calls the internal overloaded method, no need to worry about.

    Private Static void Prepare (boolean  quitallowed) {        ifnull) {              ThrowNew runtimeexception ("Only one Looper could be created per thread");        }        Sthreadlocal.set (new  Looper (quitallowed));    }

When you call the parameter is true, the system calls the time is false, in short, do not need to care.

Handler is used to let other threads modify the UI Ah, this does not need me to say.

Message processing for Android (handler related)

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.