{"id":931,"date":"2023-03-11T17:54:57","date_gmt":"2023-03-11T09:54:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=931"},"modified":"2023-04-29T15:45:12","modified_gmt":"2023-04-29T07:45:12","slug":"android-timertask-timed-task-stuck","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-timertask-timed-task-stuck\/","title":{"rendered":"Android TimerTask\u5b9a\u65f6\u4efb\u52a1\u5361\u987f"},"content":{"rendered":"<h2>\u73b0\u8c61\u63cf\u8ff0<\/h2>\n<p>Android\u4e2d\u4f7f\u7528Timer\u5f00\u542fTimerTask\u5b9a\u65f6\u4efb\u52a1\uff0c\u82e5TimerTask\u4efb\u52a1\u8fc7\u4e8e\u7e41\u91cd\uff0c\u5bb9\u6613\u9020\u6210\u5361\u987f\u3001\u91cd\u590d\u53ca\u8df3\u53d8\u73b0\u8c61<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p><!-- more --><\/p>\n<p>\u5f00\u8f9f\u65b0\u7ebf\u7a0b\uff0c\u51cf\u8f7bTimerTask\u5de5\u4f5c\u91cf\u3002\u5982\u5b9a\u65f6\u6570\u636e\u91c7\u96c6\u5b58\u50a8\uff0c\u5c06\u6570\u636e\u91c7\u96c6\u653e\u5728\u5b9a\u65f6\u4efb\u52a1\u4e2d\uff0c\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u653e\u5728\u65b0\u5f00\u8f9f\u7684\u7ebf\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u95f4\u91c7\u7528Handle\/Message\u901a\u4fe1\u673a\u5236\u3002<\/p>\n<p>\u4e3a\u51cf\u5c11Handler\u7684\u7ef4\u62a4\u5de5\u4f5c\uff0c\u53ef\u91c7\u7528\u5185\u7f6eHandler\u7684HandlerThread<\/p>\n<pre><code class=\"language-java\">mDataSaveThread = new HandlerThread(&quot;data_save&quot;);\nmDataSaveThread.start();\nmDataSaveHandler = new Handler(mDataSaveThread.getLooper()) {\n    @Override\n    public void handleMessage(Message msg) {\n        Data data = (Data) msg.obj;\n        try {\n            mDataStringBuilder.delete(0, mDataStringBuilder.length());\n            mDataStringBuilder.append...;\n            mBufw.write(mDataStringBuilder.toString());\n            mBufw.newLine();\n            mBufw.flush();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n};\n\nmTimer = new Timer();\nif (mTimerTask == null) {\n    mTimerTask = new TimerTask() {\n        @Override\n        public void run() {\n            mCount++;\n            Data data = new Data();\n            data.count = mData.count = mCount;\n            data.datetime = mData.datetime = getMilliDateTime();\n\n            ...\n\n            Message msg = mDataSaveHandler.obtainMessage();\n            msg.what = 0;\n            msg.obj = data;\n            mDataSaveHandler.sendMessage(msg);\n        }\n    };\n    mTimer.scheduleAtFixedRate(mTimerTask, 1000, PERIOD);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u8c61\u63cf\u8ff0 Android\u4e2d\u4f7f\u7528Timer\u5f00\u542fTimerTask\u5b9a\u65f6\u4efb\u52a1\uff0c\u82e5TimerTask\u4efb\u52a1\u8fc7\u4e8e\u7e41\u91cd\uff0c\u5bb9\u6613 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[213],"tags":[],"class_list":["post-931","post","type-post","status-publish","format-standard","hentry","category-android-performance"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=931"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/931\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}