{"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>\u00a0<\/p>\n<p>Trick was to create a handler in the activity that is running.<\/p>\n<p>\u00a0<\/p>\n<p>Then make a \u201cRunnable\u201d function that will do what you want. \u2014 think of this as the \u201cfunction to be run later\u201d.<\/p>\n<p>\u00a0<\/p>\n<p>To call it later issues handler.postDelayed(runnable_function,20000);<\/p>\n<p>\u00a0<\/p>\n<p>If the user does something that makes you want to \u201creset\u201d 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(), \u201cPosting Score in Background.\u201d, Toast.LENGTH_SHORT).show();<br \/>\nnew PostScoreTask().execute(S);<br \/>\n}<br \/>\n};<\/p>\n<p>\u00a0<\/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>\u00a0<\/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 \u201cQueue\u201d 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 \u201cnear Real time\u201d but not waste internet resources posting until current input has been completed.<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/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. \u00a0 Trick was to create a handler in the activity that is running. \u00a0 Then make a \u201cRunnable\u201d 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":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/101"}],"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}]}}