{"id":101,"date":"2011-07-05T05:27:51","date_gmt":"2011-07-05T02:27:51","guid":{"rendered":"http:\/\/meekconsulting.com\/?p=101"},"modified":"2011-07-05T05:27:51","modified_gmt":"2011-07-05T02:27:51","slug":"using-handler-postdelayed-to-queue-a-task-for-later-execution","status":"publish","type":"post","link":"https:\/\/meekconsulting.com\/?p=101","title":{"rendered":"Using Handler postDelayed to queue a task for later execution"},"content":{"rendered":"<p>I figured out how to get my android app to auto post after a certain amount of idle time.<\/p>\n<p>&nbsp;<\/p>\n<p>Trick was to create a handler in the activity that is running.<\/p>\n<p>&nbsp;<\/p>\n<p>Then make a &#8220;Runnable&#8221; function that will do what you want. &#8212; think of this as the &#8220;function to be run later&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p>To call it later issues handler.postDelayed(runnable_function,20000);<\/p>\n<p>&nbsp;<\/p>\n<p>If the user does something that makes you want to &#8220;reset&#8221; or cancel the pending call you can call handler.<\/p>\n<p>private Handler handler = new Handler();<\/p>\n<p>final Runnable post_score = new Runnable()<br \/>\n{<br \/>\npublic void run()<br \/>\n{<br \/>\nToast.makeText(getApplicationContext(), &#8220;Posting Score in Background.&#8221;, Toast.LENGTH_SHORT).show();<br \/>\nnew PostScoreTask().execute(S);<br \/>\n}<br \/>\n};<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Queue up a message to Post this score<br \/>\nhandler.removeCallbacks(post_score);<br \/>\nhandler.postDelayed(post_score, auto_post_delay);<\/p>\n<p>&nbsp;<\/p>\n<p>I call these two commands above any time the user changes something that should be posted to the server.\u00a0 But each time they do something we cancel any pending posts and &#8220;Queue&#8221; up another message to ask that it be done after the specified auto_post_delay. ( which is an integer set to number of milliseconds to wait before posting.<br \/>\nThis should cause the posting to be &#8220;near Real time&#8221; but not waste internet resources posting until current input has been completed.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I figured out how to get my android app to auto post after a certain amount of idle time. &nbsp; Trick was to create a handler in the activity that is running. &nbsp; Then make a &#8220;Runnable&#8221; function that will &hellip; <a href=\"https:\/\/meekconsulting.com\/?p=101\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101"}],"version-history":[{"count":1,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}