How Handlers Work on Android


Android Java Programming works through a concept of message passing, in which threads of execution in a program pass messages of executable code between one another in order to accomplish specific tasks or maintain communication between different parts of the application. The "Handler" class, along with its "MessageQueue" class, organize, execute, and dispatch messages and executable objects through the code. Each thread in an Android program can have a Handler class for the sole purpose of handling data and messages.

Android Runnable Class

  • The "Runnable" class represents an interface for the "Thread" class. Classes can implement the Runnable class so that programs can execute instances of that class as running threads. Using the Runnable interface means that a class can run concurrently with other classes, so that multiple instances of class execution can occur at the same time. Android programs, through Handlers, can pass executable objects as messages.

Android Message Class

  • The Android "Message" class facilitates the passing of data within the Android code. A Message object contains an arbitrary data object that can be passed along to a Handler object, along with programmer-defined information regarding the nature of the data inside the object. Message objects also contain methods to obtain data from other sources, serving as sort of a "passing" medium by which data and meta-data can be passed programmatically between programs and Handler objects.

Android MessageQueue and Looper Classes

  • The "MessageQueue" and "Looper" classes serve as interfaces for the processing of messages and data inside a thread of execution. The MessageQueue and Looper classes take messages dispatched to a single thread of execution. These messages are represented by objects of the Message class. The MessageQueue holds a list of these Message objects, while the Looper class cycles through them for processing by the current thread of execution.


  • A Handler object allows the programmer to send and retrieve messages and runnable objects from MessageQueues associated with a thread of execution. Message objects from other threads, or runnable threads sent to the current thread of execution, are sent to the Handler through the MessageQueue, and executed by the handler. Any outgoing messages will go through the Handler objects on their way to the MessageQueue. Each thread of execution in a program will have one Handler object associated with it, along with a single MessageQueue.

How the Handler Works

  • The Handler instance in a thread will receive Runnable and Message objects through the MessageQueue. Methods are then invoked by the programmer to handle specific objects. For example, the "post" method takes Runnable objects and puts them into the MessageQueue. Similarly, the "sendMessage" methods accomplish the same thing with Message objects. Variations of these methods specify when a Handler's MessageQueue processes Runnable or Message objects.

Related Searches


Promoted By Zergnet


Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!