Handler low.message.

Source: Internet
Author: User

Note that there are two classes: Message and Messager. In fact, the function is similar.

Each Messager has a handler.

MSGR. Send (MSG );

1. Messenger MSGR = new messenger (mhandler );

2. Public Messenger (handler target ){
Mtarget = target. getimessenger ();
}

3. Final imessenger getimessenger (){
Synchronized (mqueue ){
If (mmessenger! = NULL ){
Return mmessenger;
}
Mmessenger = new messengerimpl ();
Return mmessenger;
}
}

4. Private final class messengerimpl extends imessenger. Stub {
Public void send (Message MSG ){
Handler. This. sendmessage (MSG );
}
}

5. Public final Boolean sendmessage (Message MSG)
{
Return sendmessagedelayed (MSG, 0 );
}

6. Public Boolean sendmessageattime (Message MSG, long uptimemillis ){
Messagequeue queue = mqueue;
If (queue = NULL ){
Runtimeexception E = new runtimeexception (
This + "sendmessageattime () called with no mqueue ");
Log. W ("logoff", E. getmessage (), e );
Return false;
}
Return enqueuemessage (queue, MSG, uptimemillis );
}

Operations 3-6 are in handler. java.

Message MSG = uihandler. obtainmessage (100, backstr );

MSG. sendtotarget ();

1 public final message obtainmessage (INT what, object OBJ)
{
Return message. Obtain (this, what, OBJ );
}

2 public static message obtain (handler H, int what, object OBJ ){
Message M = obtain ();
M.tar get = h;
M. What = what;
M. OBJ = OBJ;

Return m;
}
3 Public void sendtotarget (){
Target. sendmessage (this); // send yourself
}

Post usage
Looperhandler. Post (New runnable (){

@ Override
Public void run (){
Log. I ("Glenn:", "From post! ");
}

});

1 public final Boolean post (runnable R)
{
Return sendmessagedelayed (getpostmessage (R), 0 );
}

2 Private Static message getpostmessage (runnable R ){
Message M = message. Obtain ();
M. Callback = R;
Return m;
}

3 Public void dispatchmessage (Message MSG ){
If (msg. callback! = NULL ){
Handlecallback (MSG); // call post
} Else {
If (mcallback! = NULL ){
If (mcallback. handlemessage (MSG )){
Return;
}
}
Handlemessage (MSG );
}
}
4.
Private Static void handlecallback (message ){
Message. Callback. Run ();
}

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.