Handler In Android

31 / Jul / 2015 by Himanshu Singh 0 comments

android.os.Handler allows us to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue.

Handler used for:

  • Message creation
  • Inserting messages into the queue
  • Processing messages on the consumer thread
  • Managing messages in the queue

Construction Of Handler

    1. By using Looper of the current thread
  new Handler();
  new Handler(Handler.Callback)
    1. By using explicit Looper
  new Handler(Looper);
  new Handler(Looper, Handler.Callback);

Message creation
There is a list of methods that will help to create Message

Message obtainMessage(int what, int arg1, int arg2)
Message obtainMessage()
Message obtainMessage(int what, int arg1, int arg2, Object obj)
Message obtainMessage(int what)
Message obtainMessage(int what, Object obj)

Inserting messages into the queue

    1. Add a task to the message queue
  boolean post(Runnable r)
  boolean postAtFrontOfQueue(Runnable r)
  boolean postAtTime(Runnable r, Object token, long uptimeMillis)
  boolean postAtTime(Runnable r, long uptimeMillis)
  boolean postDelayed(Runnable r, long delayMillis)
    1. Add a data object to the message queue.
  boolean sendMessage(Message msg)
  boolean sendMessageAtFrontOfQueue(Message msg)
  boolean sendMessageAtTime(Message msg, long uptimeMillis)
  boolean sendMessageDelayed(Message msg, long delayMillis)
    1. Add simple data object to the message queue.
  boolean sendEmptyMessage(int what)
  boolean sendEmptyMessageAtTime(int what, long uptimeMillis)
  boolean sendEmptyMessageDelayed(int what, long delayMillis)

Processing Message
Message is dispatched by the looper and deliver to Handler. Handler is received this message and process it. Processing Operation is done at the consumer thread for which handler is belong.

      There are two type of message in Message

    1. Task Message

There is Runnable object, not data.

  handler.post(new Runnable() {
    public void run() {
        //TODO : Do some operation
    1. Data Message

There is data,not Runnable object. Processing on messsage is performed inside of Handler.handleMessage().

  final Handler handler = new Handler() {
     public void handleMessage(Message message) {
        //TODO : Get the data from Message and perform opertation accordingly.


Removing Messages from the Queue
When we talk about removing something, The first thing that comes in our mind is that what is the identifier for it?

Message Identifiers

Identifier Description Messages to which it applies
Handler Message receiver Both task and data messages
Object Message tag Both task and data messages
Integer what parameter of Message Data messages
Runnable Task to be executed Task messages

There are the following method which is used for managing the queue –

    1. Remove a task from the message queue
  removeCallbacks(Runnable r)
  removeCallbacks(Runnable r, Object token)
    1. Remove a data message from the message queue
  removeMessages(int what)
  removeMessages(int what, Object object)
    1. Remove tasks and data messages from the message queue
  removeCallbacksAndMessages(Object token)

How to Trace the message queque processing ?

  Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, TAG));

Let’s look at an example of tracing a message

  handler.post(new Runnable() {
    public void run() {
        Log.d(TAG, "Executing Runnable");

output of this code is

  Dispatching to Handler (android.os.Handler) {8226ef40} com.shim.handlertest.activities.HandlerMessageActivity$1@46590829: 0
  Executing Runnable
  Finished to Handler (android.os.Handler) {8226ef40} com.shim.handlertest.activities.HandlerMessageActivity$1@46590829

Leave a comment -