Handler In Android
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
- By using Looper of the current thread
[java]
new Handler();
new Handler(Handler.Callback)
[/java]
- By using explicit Looper
[java]
new Handler(Looper);
new Handler(Looper, Handler.Callback);
[/java]
Message creation
There is a list of methods that will help to create Message
[java]
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)
[/java]
Inserting messages into the queue
- Add a task to the message queue
[java]
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)
[/java]
- Add a data object to the message queue.
[java]
boolean sendMessage(Message msg)
boolean sendMessageAtFrontOfQueue(Message msg)
boolean sendMessageAtTime(Message msg, long uptimeMillis)
boolean sendMessageDelayed(Message msg, long delayMillis)
[/java]
- Add simple data object to the message queue.
[java]
boolean sendEmptyMessage(int what)
boolean sendEmptyMessageAtTime(int what, long uptimeMillis)
boolean sendEmptyMessageDelayed(int what, long delayMillis)
[/java]
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.
- Task Message
- There are two type of message in Message
There is Runnable object, not data.
[java]
handler.post(new Runnable() {
@Override
public void run() {
//TODO : Do some operation
}
});
[/java]
- Data Message
There is data,not Runnable object. Processing on messsage is performed inside of Handler.handleMessage().
[java]
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
//TODO : Get the data from Message and perform opertation accordingly.
}
};
handler.sendMessage(message);
[/java]
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 –
- Remove a task from the message queue
[java]
removeCallbacks(Runnable r)
removeCallbacks(Runnable r, Object token)
[/java]
- Remove a data message from the message queue
[java]
removeMessages(int what)
removeMessages(int what, Object object)
[/java]
- Remove tasks and data messages from the message queue
[java]
removeCallbacksAndMessages(Object token)
[/java]
How to Trace the message queque processing ?
[java]
Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, TAG));
[/java]Let’s look at an example of tracing a message
[java]
handler.post(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Executing Runnable");
}
});
mHandler.sendEmptyMessage(111);
[/java]output of this code is
[java]
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
[/java]