{"id":705,"date":"2011-01-05T16:20:42","date_gmt":"2011-01-05T21:20:42","guid":{"rendered":"httpss:\/\/www.powenko.com\/wordpress\/?p=705"},"modified":"2014-11-16T22:35:38","modified_gmt":"2014-11-17T03:35:38","slug":"android-ui-timer-timer-with-ui-%e8%a8%88%e6%99%82%e5%99%a8%e8%a3%a1%e9%9d%a2%e8%a6%81%e6%8e%a7%e5%88%b6ui-%e7%9a%84%e8%b3%87%e6%96%99","status":"publish","type":"post","link":"https:\/\/www.powenko.com\/wordpress\/?p=705","title":{"rendered":"android, UI, timer, timer with UI  \u8a08\u6642\u5668\u88e1\u9762\u8981\u63a7\u5236UI \u7684\u8cc7\u6599"},"content":{"rendered":"<p>Updating the UI from a Timer<\/p>\n<pre class=\"Java\" cols=\"60\" rows=\"10\" name=\"code\">    \r\npublic class myActivity extends Activity {\r\n\r\n\tprivate Timer myTimer;\r\n\r\n\t\/** Called when the activity is first created. *\/\r\n\t@Override\r\n\tpublic void onCreate(Bundle icicle) {\r\n\t\tsuper.onCreate(icicle);\r\n\t\tsetContentView(R.layout.main);\r\n\r\n\t\tmyTimer = new Timer();\r\n\t\tmyTimer.schedule(new TimerTask() {\r\n\t\t\t@Override\r\n\t\t\tpublic void run() {\r\n\t\t\t\tTimerMethod();\r\n\t\t\t}\r\n\r\n\t\t}, 0, 1000);\r\n\t}\r\n\r\n\tprivate void TimerMethod()\r\n\t{\r\n\t\t\/\/This method is called directly by the timer\r\n\t\t\/\/and runs in the same thread as the timer.\r\n\r\n\t\t\/\/We call the method that will work with the UI\r\n\t\t\/\/through the runOnUiThread method.\r\n\t\tthis.runOnUiThread(Timer_Tick);\r\n\t}\r\n\r\n\tprivate Runnable Timer_Tick = new Runnable() {\r\n\t\tpublic void run() {\r\n\r\n\t\t\/\/This method runs in the same thread as the UI.    \t       \r\n\r\n\t\t\/\/Do something to the UI thread here\r\n\r\n\t\t}\r\n\t};\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Updating the UI from a Timer public class myActivity ex [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4869,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-705","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-threadtimer"],"_links":{"self":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/705"}],"collection":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=705"}],"version-history":[{"count":9,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":707,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions\/707"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/media\/4869"}],"wp:attachment":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}