{"id":24756,"date":"2015-07-31T23:52:56","date_gmt":"2015-07-31T18:22:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24756"},"modified":"2015-08-03T11:51:45","modified_gmt":"2015-08-03T06:21:45","slug":"handler-in-android","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/handler-in-android\/","title":{"rendered":"Handler In Android"},"content":{"rendered":"<p>android.os.Handler allows us to send and process Message and Runnable objects associated with a thread&#8217;s MessageQueue. Each Handler instance is associated with a single thread and that thread&#8217;s message queue.<\/p>\n<p>Handler used for:<\/p>\n<ul>\n<li>Message creation<\/li>\n<li>Inserting messages into the queue<\/li>\n<li>Processing messages on the consumer thread<\/li>\n<li>Managing messages in the queue<\/li>\n<\/ul>\n<p><strong>Construction Of Handler<\/strong><\/p>\n<ol>\n<ol>\n<li>By using <em>Looper<\/em> of the current thread<\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  new Handler();<br \/>\n  new Handler(Handler.Callback)<br \/>\n  [\/java]<\/p>\n<ol>\n<ol>\n<li>By using <em>explicit Looper<\/em><\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  new Handler(Looper);<br \/>\n  new Handler(Looper, Handler.Callback);<br \/>\n  [\/java]<\/p>\n<p><strong>Message creation<\/strong><br \/>\nThere is a list of methods that will help to create Message<\/p>\n<p>[java]<br \/>\nMessage obtainMessage(int what, int arg1, int arg2)<br \/>\nMessage obtainMessage()<br \/>\nMessage obtainMessage(int what, int arg1, int arg2, Object obj)<br \/>\nMessage obtainMessage(int what)<br \/>\nMessage obtainMessage(int what, Object obj)<br \/>\n[\/java]<\/p>\n<p><strong>Inserting messages into the queue<\/strong><\/p>\n<ol>\n<ol>\n<li>Add a task to the message queue<\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  boolean post(Runnable r)<br \/>\n  boolean postAtFrontOfQueue(Runnable r)<br \/>\n  boolean postAtTime(Runnable r, Object token, long uptimeMillis)<br \/>\n  boolean postAtTime(Runnable r, long uptimeMillis)<br \/>\n  boolean postDelayed(Runnable r, long delayMillis)<br \/>\n  [\/java]<\/p>\n<ol>\n<ol>\n<li>Add a data object to the message queue.<\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  boolean sendMessage(Message msg)<br \/>\n  boolean sendMessageAtFrontOfQueue(Message msg)<br \/>\n  boolean sendMessageAtTime(Message msg, long uptimeMillis)<br \/>\n  boolean sendMessageDelayed(Message msg, long delayMillis)<br \/>\n  [\/java]<\/p>\n<ol>\n<ol>\n<li>Add simple data object to the message queue.<\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  boolean sendEmptyMessage(int what)<br \/>\n  boolean sendEmptyMessageAtTime(int what, long uptimeMillis)<br \/>\n  boolean sendEmptyMessageDelayed(int what, long delayMillis)<br \/>\n  [\/java]<\/p>\n<p><strong>Processing Message<\/strong><br \/>\nMessage 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.<\/p>\n<ol>\n<ol>There are two type of message in Message<\/p>\n<li><strong>Task Message<\/strong><\/li>\n<\/ol>\n<\/ol>\n<p>There is Runnable object, not data.<\/p>\n<p>[java]<br \/>\n  handler.post(new Runnable() {<br \/>\n    @Override<br \/>\n    public void run() {<br \/>\n        \/\/TODO : Do some operation<br \/>\n    }<br \/>\n });<br \/>\n  [\/java]<\/p>\n<ol>\n<ol>\n<li><strong>Data Message<\/strong><\/li>\n<\/ol>\n<\/ol>\n<p>There is data,not Runnable object. Processing on messsage is performed inside of <em>Handler.handleMessage()<\/em>.<\/p>\n<p>[java]<br \/>\n  final Handler handler = new Handler() {<br \/>\n     @Override<br \/>\n     public void handleMessage(Message message) {<br \/>\n        \/\/TODO : Get the data from Message and perform opertation accordingly.<br \/>\n     }<br \/>\n };<\/p>\n<p>handler.sendMessage(message);<br \/>\n  [\/java]<\/p>\n<p><strong>Removing Messages from the Queue<\/strong><br \/>\nWhen we talk about removing something, The first thing that comes in our mind is that what is the identifier for it?<\/p>\n<p>Message Identifiers<\/p>\n<table style=\"width: 100%\" border=\"1\">\n<tbody>\n<tr>\n<th>Identifier<\/th>\n<th>Description<\/th>\n<th>Messages to which it applies<\/th>\n<\/tr>\n<tr>\n<td>Handler<\/td>\n<td>Message receiver<\/td>\n<td>Both task and data messages<\/td>\n<\/tr>\n<tr>\n<td>Object<\/td>\n<td>Message tag<\/td>\n<td>Both task and data messages<\/td>\n<\/tr>\n<tr>\n<td>Integer<\/td>\n<td><em>what<\/em> parameter of Message<\/td>\n<td>Data messages<\/td>\n<\/tr>\n<tr>\n<td>Runnable<\/td>\n<td>Task to be executed<\/td>\n<td>Task messages<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are the following method which is used for managing the queue &#8211;<\/p>\n<ol>\n<ol>\n<li>Remove a task from the message queue<\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  removeCallbacks(Runnable r)<br \/>\n  removeCallbacks(Runnable r, Object token)<br \/>\n  [\/java]<\/p>\n<ol>\n<ol>\n<li>Remove a data message from the message queue<\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  removeMessages(int what)<br \/>\n  removeMessages(int what, Object object)<br \/>\n  [\/java]<\/p>\n<ol>\n<ol>\n<li>Remove tasks and data messages from the message queue<\/li>\n<\/ol>\n<\/ol>\n<p>[java]<br \/>\n  removeCallbacksAndMessages(Object token)<br \/>\n  [\/java]<\/p>\n<blockquote><p><strong>How to Trace the message queque processing ?<\/strong><\/p>\n<p>[java]<br \/>\n  Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, TAG));<br \/>\n  [\/java]<\/p>\n<p>Let\u2019s look at an example of tracing a message<\/p>\n<p>[java]<br \/>\n  handler.post(new Runnable() {<br \/>\n    @Override<br \/>\n    public void run() {<br \/>\n        Log.d(TAG, &quot;Executing Runnable&quot;);<br \/>\n    }<br \/>\n  });<br \/>\n  mHandler.sendEmptyMessage(111);<br \/>\n  [\/java]<\/p>\n<p>output of this code is<\/p>\n<p>[java]<br \/>\n  Dispatching to Handler (android.os.Handler) {8226ef40} com.shim.handlertest.activities.HandlerMessageActivity$1@46590829: 0<br \/>\n  Executing Runnable<br \/>\n  Finished to Handler (android.os.Handler) {8226ef40} com.shim.handlertest.activities.HandlerMessageActivity$1@46590829<br \/>\n  [\/java]<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>android.os.Handler allows us to send and process Message and Runnable objects associated with a thread&#8217;s MessageQueue. Each Handler instance is associated with a single thread and that thread&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":126,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":30},"categories":[518,1],"tags":[2086,2088,2085,2087],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24756"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/126"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24756"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24756\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}