{"id":1679,"date":"2016-08-16T11:51:01","date_gmt":"2016-08-16T03:51:01","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1679"},"modified":"2016-08-16T11:51:01","modified_gmt":"2016-08-16T03:51:01","slug":"android%e4%b9%8bhandler%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e5%88%86%e6%9e%90%e5%8f%8a%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1679","title":{"rendered":"Android\u4e4bHandler\u5185\u5b58\u6cc4\u6f0f\u5206\u6790\u53ca\u89e3\u51b3"},"content":{"rendered":"<h3>\u4e00\u3001\u4ecb\u7ecd<\/h3>\n<p>\u9996\u5148\uff0c\u8bf7\u6d4f\u89c8\u4e0b\u9762\u8fd9\u6bb5handler\u4ee3\u7801\uff1a<\/p>\n<pre class=\"hljs scala\"><code class=\"scala\">public <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SampleActivity<\/span> <span class=\"hljs-keyword\"><span class=\"hljs-keyword\">extends<\/span><\/span> <span class=\"hljs-title\">Activity<\/span> {<\/span>\r\n  <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">Handler<\/span> mLeakyHandler = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-type\">Handler<\/span>() {\r\n    <span class=\"hljs-annotation\">@Override<\/span>\r\n    public void handleMessage(<span class=\"hljs-type\">Message<\/span> msg) {\r\n      <span class=\"hljs-comment\">\/\/ ... <\/span>\r\n    }\r\n  }\r\n}<\/code><\/pre>\n<p>\u5728\u4f7f\u7528handler\u65f6\uff0c\u8fd9\u662f\u4e00\u6bb5\u5f88\u5e38\u89c1\u7684\u4ee3\u7801\u3002\u4f46\u662f\uff0c\u5b83\u5374\u4f1a\u9020\u6210\u4e25\u91cd\u7684\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002\u5728\u5b9e\u9645\u7f16\u5199\u4e2d\uff0c\u6211\u4eec\u5f80\u5f80\u4f1a\u5f97\u5230\u5982\u4e0b\u8b66\u544a\uff1a<\/p>\n<pre class=\"hljs sql\"><code class=\"sql\"> \u26a0 In Android, <span class=\"hljs-operator\"><span class=\"hljs-keyword\">Handler<\/span> classes should be <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">or<\/span> leaks might occur.<\/span><\/code><\/pre>\n<p>\u90a3\u4e48\uff0chandler\u662f\u5982\u4f55\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u7684\u5462\uff1f<\/p>\n<h3>\u4e8c\u3001\u5206\u6790<\/h3>\n<h5>1\u3001 Android\u89d2\u5ea6<\/h5>\n<p>\u5f53Android\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0cframework\u4f1a\u4e3a\u8be5\u5e94\u7528\u7a0b\u5e8f\u7684<code>\u4e3b\u7ebf\u7a0b<\/code>\u521b\u5efa\u4e00\u4e2a<code>Looper<\/code>\u5bf9\u8c61\u3002\u8fd9\u4e2aLooper\u5bf9\u8c61\u5305\u542b\u4e00\u4e2a\u7b80\u5355\u7684<code>\u6d88\u606f\u961f\u5217Message Queue<\/code>\uff0c\u5e76\u4e14\u80fd\u591f\u5faa\u73af\u7684\u5904\u7406\u961f\u5217\u4e2d\u7684<a href=\"http:\/\/developer.android.com\/reference\/android\/os\/Message.html\" target=\"_blank\">\u6d88\u606f<\/a>\u3002\u8fd9\u4e9b\u6d88\u606f\u5305\u62ec\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8fframework\u4e8b\u4ef6\uff0c\u4f8b\u5982Activity\u751f\u547d\u5468\u671f\u65b9\u6cd5\u8c03\u7528\u3001button\u70b9\u51fb\u7b49\uff0c\u8fd9\u4e9b\u6d88\u606f\u90fd\u4f1a\u88ab\u6dfb\u52a0\u5230\u6d88\u606f\u961f\u5217\u4e2d\u5e76\u88ab\u9010\u4e2a\u5904\u7406\u3002<br \/>\n\u53e6\u5916\uff0c\u4e3b\u7ebf\u7a0b\u7684Looper\u5bf9\u8c61\u4f1a\u4f34\u968f\u8be5\u5e94\u7528\u7a0b\u5e8f\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>\u7136\u540e\uff0c\u5f53\u4e3b\u7ebf\u7a0b\u91cc\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2a<code>Handler<\/code>\u5bf9\u8c61\u540e\uff0c\u5b83\u5c31\u4f1a\u81ea\u52a8\u4e0e<code>\u4e3b\u7ebf\u7a0bLooper<\/code>\u7684\u6d88\u606f\u961f\u5217<code>\u5173\u8054<\/code>\u8d77\u6765\u3002\u6240\u6709\u53d1\u9001\u5230\u6d88\u606f\u961f\u5217\u7684\u6d88\u606fMessage\u90fd\u4f1a\u62e5\u6709\u4e00\u4e2a<code>\u5bf9Handler\u7684\u5f15\u7528<\/code>\uff0c\u6240\u4ee5\u5f53Looper\u6765\u5904\u7406\u6d88\u606f\u65f6\uff0c\u4f1a\u636e\u6b64\u56de\u8c03[Handler#handleMessage(Message)](<a href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#handleMessage(android.os.Message)%E6%96%B9%E6%B3%95%E6%9D%A5%E5%A4%84%E7%90%86%E6%B6%88%E6%81%AF%E3%80%82\" target=\"_blank\">http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#handleMessage(android.os.Message)\u65b9\u6cd5\u6765\u5904\u7406\u6d88\u606f\u3002<\/a><\/p>\n<h5>2\u3001 Java\u89d2\u5ea6<\/h5>\n<p>\u5728java\u91cc\uff0c<code>\u975e\u9759\u6001\u5185\u90e8\u7c7b<\/code> \u548c <code>\u533f\u540d\u7c7b<\/code> \u90fd\u4f1a\u6f5c\u5728\u7684\u5f15\u7528\u5b83\u4eec\u6240\u5c5e\u7684\u5916\u90e8\u7c7b\u3002\u4f46\u662f\uff0c<code>\u9759\u6001\u5185\u90e8\u7c7b<\/code>\u5374\u4e0d\u4f1a\u3002<\/p>\n<h3>\u4e09\u3001\u6cc4\u6f0f\u6765\u6e90<\/h3>\n<p>\u8bf7\u6d4f\u89c8\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"hljs scala\"><code class=\"scala\">public <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SampleActivity<\/span> <span class=\"hljs-keyword\"><span class=\"hljs-keyword\">extends<\/span><\/span> <span class=\"hljs-title\">Activity<\/span> {<\/span>\r\n\r\n  <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">Handler<\/span> mLeakyHandler = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-type\">Handler<\/span>() {\r\n    <span class=\"hljs-annotation\">@Override<\/span>\r\n    public void handleMessage(<span class=\"hljs-type\">Message<\/span> msg) {\r\n      <span class=\"hljs-comment\">\/\/ ...<\/span>\r\n    }\r\n  }\r\n\r\n  <span class=\"hljs-annotation\">@Override<\/span>\r\n  <span class=\"hljs-keyword\">protected<\/span> void onCreate(<span class=\"hljs-type\">Bundle<\/span> savedInstanceState) {\r\n    <span class=\"hljs-keyword\">super<\/span>.onCreate(savedInstanceState);\r\n\r\n    <span class=\"hljs-comment\">\/\/ Post a message and delay its execution for 10 minutes.<\/span>\r\n    mLeakyHandler.postDelayed(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-type\">Runnable<\/span>() {\r\n      <span class=\"hljs-annotation\">@Override<\/span>\r\n      public void run() { <span class=\"hljs-comment\">\/* ... *\/<\/span> }\r\n    }, <span class=\"hljs-number\">1000<\/span> * <span class=\"hljs-number\">60<\/span> * <span class=\"hljs-number\">10<\/span>);\r\n\r\n    <span class=\"hljs-comment\">\/\/ Go back to the previous Activity.<\/span>\r\n    finish();\r\n  }\r\n}<\/code><\/pre>\n<p>\u5f53activity\u7ed3\u675f(finish)\u65f6\uff0c\u91cc\u9762\u7684\u5ef6\u65f6\u6d88\u606f\u5728\u5f97\u5230\u5904\u7406\u524d\uff0c\u4f1a\u4e00\u76f4\u4fdd\u5b58\u5728\u4e3b\u7ebf\u7a0b\u7684\u6d88\u606f\u961f\u5217\u91cc\u6301\u7eed10\u5206\u949f\u3002\u800c\u4e14\uff0c\u7531\u4e0a\u6587\u53ef\u77e5\uff0c<code>\u8fd9\u6761\u6d88\u606f\u6301\u6709\u5bf9handler\u7684\u5f15\u7528<\/code>\uff0c\u800c<code>handler\u53c8\u6301\u6709\u5bf9\u5176\u5916\u90e8\u7c7b\uff08\u5728\u8fd9\u91cc\uff0c\u5373SampleActivity\uff09\u7684\u6f5c\u5728\u5f15\u7528<\/code>\u3002\u8fd9\u6761\u5f15\u7528\u5173\u7cfb\u4f1a\u4e00\u76f4\u4fdd\u6301\u76f4\u5230\u6d88\u606f\u5f97\u5230\u5904\u7406\uff0c\u4ece\u800c\uff0c\u8fd9<code>\u963b\u6b62\u4e86SampleActivity\u88ab\u5783\u573e\u56de\u6536\u5668\u56de\u6536<\/code>\uff0c\u540c\u65f6\u9020\u6210\u5e94\u7528\u7a0b\u5e8f\u7684<code>\u6cc4\u6f0f<\/code>\u3002<br \/>\n\u6ce8\u610f\uff0c\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684<code>Runnable\u7c7b<\/code>&#8212;<code>\u975e\u9759\u6001\u533f\u540d\u7c7b<\/code>&#8211;\u540c\u6837\u6301\u6709\u5bf9\u5176\u5916\u90e8\u7c7b\u7684\u5f15\u7528\u3002\u4ece\u800c\u4e5f\u5bfc\u81f4\u6cc4\u6f0f\u3002<\/p>\n<h3>\u56db\u3001\u6cc4\u6f0f\u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u9996\u5148\uff0c\u4e0a\u9762\u5df2\u7ecf\u660e\u786e\u4e86\u5185\u5b58\u6cc4\u6f0f\u6765\u6e90\uff1a<\/p>\n<ol>\n<li>\u53ea\u8981\u6709\u672a\u5904\u7406\u7684\u6d88\u606f\uff0c\u90a3\u4e48\u6d88\u606f\u4f1a\u5f15\u7528handler\uff0c\u975e\u9759\u6001\u7684handler\u53c8\u4f1a\u5f15\u7528\u5916\u90e8\u7c7b\uff0c\u5373Activity\uff0c\u5bfc\u81f4Activity\u65e0\u6cd5\u88ab\u56de\u6536\uff0c\u9020\u6210\u6cc4\u6f0f\uff1b<\/li>\n<li>Runnable\u7c7b\u5c5e\u4e8e\u975e\u9759\u6001\u533f\u540d\u7c7b\uff0c\u540c\u6837\u4f1a\u5f15\u7528\u5916\u90e8\u7c7b\u3002<br \/>\n\u4e3a\u4e86\u89e3\u51b3\u9047\u5230\u7684\u95ee\u9898\uff0c\u6211\u4eec\u8981\u660e\u786e\u4e00\u70b9\uff1a<code>\u9759\u6001\u5185\u90e8\u7c7b\u4e0d\u4f1a\u6301\u6709\u5bf9\u5916\u90e8\u7c7b\u7684\u5f15\u7528<\/code>\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u53ef\u4ee5\u628ahandler\u7c7b\u653e\u5728\u5355\u72ec\u7684\u7c7b\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u4f7f\u7528<code>\u9759\u6001\u5185\u90e8\u7c7b<\/code>\u4fbf\u53ef\u4ee5\u907f\u514d\u6cc4\u6f0f\u3002<br \/>\n\u53e6\u5916\uff0c\u5982\u679c\u60f3\u8981\u5728handler\u5185\u90e8\u53bb\u8c03\u7528\u6240\u5728\u7684\u5916\u90e8\u7c7bActivity\uff0c\u90a3\u4e48\u53ef\u4ee5\u5728handler\u5185\u90e8\u4f7f\u7528\u5f31\u5f15\u7528\u7684\u65b9\u5f0f\u6307\u5411\u6240\u5728Activity\uff0c\u8fd9\u6837\u7edf\u4e00\u4e0d\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<br \/>\n\u5bf9\u4e8e\u533f\u540d\u7c7bRunnable\uff0c\u540c\u6837\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a<code>\u9759\u6001\u7c7b<\/code>\u3002\u56e0\u4e3a\u9759\u6001\u7684\u533f\u540d\u7c7b\u4e0d\u4f1a\u6301\u6709\u5bf9\u5916\u90e8\u7c7b\u7684\u5f15\u7528\u3002<\/li>\n<\/ol>\n<pre class=\"hljs scala\"><code class=\"scala\">public <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SampleActivity<\/span> <span class=\"hljs-keyword\"><span class=\"hljs-keyword\">extends<\/span><\/span> <span class=\"hljs-title\">Activity<\/span> {<\/span>\r\n\r\n  <span class=\"hljs-comment\">\/**\r\n   * Instances of static inner classes do not hold an implicit\r\n   * reference to their outer class.\r\n   *\/<\/span>\r\n  <span class=\"hljs-keyword\">private<\/span> static <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyHandler<\/span> <span class=\"hljs-keyword\"><span class=\"hljs-keyword\">extends<\/span><\/span> <span class=\"hljs-title\">Handler<\/span> {<\/span>\r\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">WeakReference<\/span>&lt;<span class=\"hljs-type\">SampleActivity<\/span>&gt; mActivity;\r\n\r\n    public <span class=\"hljs-type\">MyHandler<\/span>(<span class=\"hljs-type\">SampleActivity<\/span> activity) {\r\n      mActivity = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-type\">WeakReference<\/span>&lt;<span class=\"hljs-type\">SampleActivity<\/span>&gt;(activity);\r\n    }\r\n\r\n    <span class=\"hljs-annotation\">@Override<\/span>\r\n    public void handleMessage(<span class=\"hljs-type\">Message<\/span> msg) {\r\n      <span class=\"hljs-type\">SampleActivity<\/span> activity = mActivity.get();\r\n      <span class=\"hljs-keyword\">if<\/span> (activity != <span class=\"hljs-literal\">null<\/span>) {\r\n        <span class=\"hljs-comment\">\/\/ ...<\/span>\r\n      }\r\n    }\r\n  }\r\n\r\n  <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">MyHandler<\/span> mHandler = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-type\">MyHandler<\/span>(<span class=\"hljs-keyword\">this<\/span>);\r\n\r\n  <span class=\"hljs-comment\">\/**\r\n   * Instances of anonymous classes do not hold an implicit\r\n   * reference to their outer class when they are \"static\".\r\n   *\/<\/span>\r\n  <span class=\"hljs-keyword\">private<\/span> static <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">Runnable<\/span> sRunnable = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-type\">Runnable<\/span>() {\r\n      <span class=\"hljs-annotation\">@Override<\/span>\r\n      public void run() { <span class=\"hljs-comment\">\/* ... *\/<\/span> }\r\n  };\r\n\r\n  <span class=\"hljs-annotation\">@Override<\/span>\r\n  <span class=\"hljs-keyword\">protected<\/span> void onCreate(<span class=\"hljs-type\">Bundle<\/span> savedInstanceState) {\r\n    <span class=\"hljs-keyword\">super<\/span>.onCreate(savedInstanceState);\r\n\r\n    <span class=\"hljs-comment\">\/\/ Post a message and delay its execution for 10 minutes.<\/span>\r\n    mHandler.postDelayed(sRunnable, <span class=\"hljs-number\">1000<\/span> * <span class=\"hljs-number\">60<\/span> * <span class=\"hljs-number\">10<\/span>);\r\n\r\n    <span class=\"hljs-comment\">\/\/ Go back to the previous Activity.<\/span>\r\n    finish();\r\n  }\r\n}<\/code><\/pre>\n<h3>\u4e94\u3001\u5c0f\u7ed3<\/h3>\n<p>\u867d\u7136\u9759\u6001\u7c7b\u4e0e\u975e\u9759\u6001\u7c7b\u4e4b\u95f4\u7684\u533a\u522b\u5e76\u4e0d\u5927\uff0c\u4f46\u662f\u5bf9\u4e8eAndroid\u5f00\u53d1\u8005\u800c\u8a00\u5374\u662f\u5fc5\u987b\u7406\u89e3\u7684\u3002\u81f3\u5c11\u6211\u4eec\u8981\u6e05\u695a\uff0c\u5982\u679c\u4e00\u4e2a\u5185\u90e8\u7c7b\u5b9e\u4f8b\u7684\u751f\u547d\u5468\u671f\u6bd4Activity\u66f4\u957f\uff0c\u90a3\u4e48\u6211\u4eec\u5343\u4e07\u4e0d\u8981\u4f7f\u7528\u975e\u9759\u6001\u7684\u5185\u90e8\u7c7b\u3002\u6700\u597d\u7684\u505a\u6cd5\u662f\uff0c\u4f7f\u7528\u9759\u6001\u5185\u90e8\u7c7b\uff0c\u7136\u540e\u5728\u8be5\u7c7b\u91cc\u4f7f\u7528\u5f31\u5f15\u7528\u6765\u6307\u5411\u6240\u5728\u7684Activity\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u539f\u6587\u94fe\u63a5\uff1ahttp:\/\/www.jianshu.com\/p\/cb9b4b71a820<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ecb\u7ecd \u9996\u5148\uff0c\u8bf7\u6d4f\u89c8\u4e0b\u9762\u8fd9\u6bb5handler\u4ee3\u7801\uff1a public class SampleActivity e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1679"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1679"}],"version-history":[{"count":1,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1679\/revisions"}],"predecessor-version":[{"id":1680,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1679\/revisions\/1680"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}