{"id":355,"date":"2011-12-06T08:41:21","date_gmt":"2011-12-06T13:41:21","guid":{"rendered":"http:\/\/www.thaylin.com\/blog\/?p=355"},"modified":"2011-12-06T08:41:21","modified_gmt":"2011-12-06T13:41:21","slug":"android-timer-stuff","status":"publish","type":"post","link":"https:\/\/www.thaylin.com\/blog\/2011\/12\/06\/android-timer-stuff\/","title":{"rendered":"Android timer stuff"},"content":{"rendered":"<p>Wow, it&#8217;s been a while since I&#8217;ve last posted!<\/p>\n<p>Just wanted to toss up some code for creating a timer in android. One would think you would just utilize the java.utils.Timer class but that creates a new thread and can create issues with your application. Plus it&#8217;s a pain to access stuff from the timer. Android has an alternative to keep everything all together and it&#8217;s quite easy to implement.<\/p>\n<pre lang=\"java\" line=\"1\" colla=\"+\">\r\nprivate Handler timerHandler;\r\nprivate int timerTime = 5000; \/\/in milliseconds\r\nprivate void createTimer()\r\n{\r\n\ttimerHandler = new Handler();\r\n\r\n}\r\nprivate void startTimer()\r\n{\r\n\t\/\/resets timer and posts the delayed call\r\n\trotationHandler.removeCallbacks(mUpdateTimeTask);\r\n\trotationHandler.postDelayed(mUpdateTimeTask, timerTime);\r\n}\r\nprivate void stopTimer()\r\n{\r\n\trotationHandler.removeCallbacks(mUpdateTimeTask);\r\n}\r\nprivate Runnable mUpdateTimeTask = new Runnable()\r\n{\r\n\t   public void run() {\r\n\t       \/\/do your thing\r\n\r\n\t\t\/\/repeat timer if you need to\r\n\t\trotationHandler.postDelayed(this, timerTime);\r\n\t\t\r\n\t   }\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Wow, it&#8217;s been a while since I&#8217;ve last posted! Just wanted to toss up some code for creating a timer in android. One would think you would just utilize the java.utils.Timer class but that creates a new thread and can create issues with your application. Plus it&#8217;s a pain to access stuff from the timer. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,29,8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/355"}],"collection":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":1,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":356,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/355\/revisions\/356"}],"wp:attachment":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}