what are handlers?

satya - Monday, May 04, 2009 4:08:38 PM

This document is by Taisa Cristina

This document is by Taisa Cristina

satya - Monday, May 04, 2009 4:09:06 PM

Introduction

If your application needs threads to communicate one to another you should consider using Handlers. A thread that has an associated message Looper and has created Handlers is able to receive Messages sent by means of its Handlers from other threads. Such messages are added to the MessageQueue attached to the thread. When the message Looper pulls a Message from the MessageQueue, that Message is dispatched to its target Handler.

satya - Monday, May 04, 2009 4:09:31 PM

Message

A Message can be sent from a thread to another (or to itself) by using a Handler. When creating a Message, one should set its identity by using the what field, so that the recipient could understand what such message is about. One may also populate the message with extra data (two int -arg1 and arg2- and an object -obj- field are available). When a Handler is used to send a Message, it's set as the Message target.

satya - Monday, May 04, 2009 4:09:57 PM

Handler

A Handler allows another thread to send a Message to the Handler's creater thread. When a Handler is requested to send a Message, such Message is in fact added to the MessageQueue associated to the Handler's thread, and it will be treated by the Handler when the Looper pulls the Message from the queue and dispatch it to him.

satya - Monday, May 04, 2009 4:10:21 PM

MessageQueue

A MessageQueue keeps all Messages sent to its associated thread. While all the Handlers push Messages into this queue, the Looper pulls them out.

satya - Monday, May 04, 2009 4:10:54 PM

Looper

A Looper runs a message loop for a thread. When a thread is attached to a Looper, it's also associated to a MessageQueue. The message loop just pulls Messages from the queue and dispatch them to its target Handler.

satya - Monday, May 04, 2009 4:11:28 PM

UI Thread and Looper

Android already attachs a Looper to the Ui thread, but common threads do not have a message Looper by default. It can be done like shown below:

satya - Monday, May 04, 2009 4:19:41 PM

Example


class MyLooperThread extends Thread 
{    
   class MyHandler extends Handler 
   {  
      @Override        
      public void handleMessage(Message msg) 
      {           
         // processing the incoming Message dispatched by the Looper 
         switch(msg.what) 
         { 
            case TYPE_01:     // do stuff related to TYPE_01 messages                    
               break;                
            case TYPE_02:     // do stuff related to TYPE_02 messages                    
               break;                
            (...)               
            default:                    
               break; 
         } 
      } 
   }     
   public MyHandler mHandler;  
   public void run() 
   {        
      // attaching a Message Looper to this thread           
      // here a MessageQueue is also associated to this thread 
      Looper.prepare();         
      // creating a Message Handler to this thread 
      mHandler = new MyHandler();         // running the loop        
      // here the Looper begins pulling Messages 
      // from the MessageQueue and dispatching them to the Handler 
      Looper.loop();    
   }
}

class OtherThread extends Thread 
{
    public void run() 
   {
        // do stuff
        (...)
        // sending a message to MyLooperThread which identity is TYPE_01
        // this Message will be enqueued to the MessageQueue
        mHandler.sendMessage(Message.obtain(mHandler, TYPE_01);
       (...)
    }  
}