{"id":4552,"date":"2021-09-01T11:17:29","date_gmt":"2021-09-01T03:17:29","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=4552"},"modified":"2025-01-08T22:02:45","modified_gmt":"2025-01-08T14:02:45","slug":"%e6%ba%90%e7%a0%81%e5%90%84%e4%b8%aa%e5%87%bb%e7%a0%b4%e4%b9%8b-handler","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552","title":{"rendered":"\u6e90\u7801\u5404\u4e2a\u51fb\u7834\u4e4b-Handler"},"content":{"rendered":"<p>\u5728\u9762\u8bd5\u7684\u8fc7\u7a0b\u4e2d,\u53cd\u590d\u7684\u88ab\u95ee\u5230Handler\u673a\u5236,\u5728\u6b64\u5bf9\u5176\u505a\u4e00\u4e2a\u6df1\u5165\u7684\u5256\u6790.\u4e0b\u9762Handler\u7684\u6e90\u7801\u90fd\u662f\u53c2\u7167API28\u7684\u3002<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#Google%E6%BA%90%E7%A0%81%E5%AF%B9Handler%E7%9A%84%E5%AE%9A%E4%B9%89\" title=\"Google\u6e90\u7801\u5bf9Handler\u7684\u5b9a\u4e49\">Google\u6e90\u7801\u5bf9Handler\u7684\u5b9a\u4e49<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#%E4%B8%80%E5%85%88%E7%9C%8B%E7%9C%8B%E6%88%91%E4%BB%AC%E5%B9%B3%E6%97%B6%E6%80%8E%E4%B9%88%E4%BD%BF%E7%94%A8Handler\" title=\"\u4e00.\u5148\u770b\u770b\u6211\u4eec\u5e73\u65f6\u600e\u4e48\u4f7f\u7528Handler\">\u4e00.\u5148\u770b\u770b\u6211\u4eec\u5e73\u65f6\u600e\u4e48\u4f7f\u7528Handler<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#%E4%BA%8C%E9%82%A3%E6%80%8E%E4%B9%88Handler%E5%B0%B1%E8%83%BD%E5%AE%9E%E7%8E%B0%E5%88%B7%E6%96%B0UI%E7%9A%84%E5%91%A2\" title=\"\u4e8c.\u90a3\u600e\u4e48Handler\u5c31\u80fd\u5b9e\u73b0\u5237\u65b0UI\u7684\u5462\">\u4e8c.\u90a3\u600e\u4e48Handler\u5c31\u80fd\u5b9e\u73b0\u5237\u65b0UI\u7684\u5462<\/a><ul class=\"ez-toc-list-level-2\"><li class=\"ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#1Handler%E5%88%9B%E5%BB%BA\" title=\"1.Handler\u521b\u5efa\">1.Handler\u521b\u5efa<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#1%EF%BC%89%E9%80%9A%E8%BF%87%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E5%88%9B%E5%BB%BA\" title=\"1\uff09\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u521b\u5efa\">1\uff09\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u521b\u5efa<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#%E5%AD%90%E7%BA%BF%E7%A8%8B%E4%B8%AD%E5%88%9B%E5%BB%BAHandler\" title=\"\u5b50\u7ebf\u7a0b\u4e2d\u521b\u5efaHandler\">\u5b50\u7ebf\u7a0b\u4e2d\u521b\u5efaHandler<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#%E5%9C%A8Activity%E9%87%8C%E7%9B%B4%E6%8E%A5%E5%88%9B%E5%BB%BAHandler\" title=\"\u5728Activity\u91cc\u76f4\u63a5\u521b\u5efaHandler\">\u5728Activity\u91cc\u76f4\u63a5\u521b\u5efaHandler<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#2%EF%BC%89%E9%80%9A%E8%BF%87%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95%E5%88%9B%E5%BB%BA\" title=\"2\uff09\u901a\u8fc7\u9759\u6001\u65b9\u6cd5\u521b\u5efa\">2\uff09\u901a\u8fc7\u9759\u6001\u65b9\u6cd5\u521b\u5efa<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#2Handler%E4%BD%BF%E7%94%A8\" title=\"2.Handler\u4f7f\u7528\">2.Handler\u4f7f\u7528<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#1%EF%BC%89%E6%89%A7%E8%A1%8CRunnable\" title=\"1\uff09\u6267\u884cRunnable\">1\uff09\u6267\u884cRunnable<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#2%EF%BC%89%E5%8F%91%E9%80%81Message\" title=\"2\uff09\u53d1\u9001Message\">2\uff09\u53d1\u9001Message<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#MessageQueue%EF%BC%88%E9%9A%BE%E7%82%B9%EF%BC%89\" title=\"MessageQueue\uff08\u96be\u70b9\uff09\">MessageQueue\uff08\u96be\u70b9\uff09<\/a><\/li><\/ul><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#3%E8%A8%80%E5%BD%92%E6%AD%A3%E4%BC%A0\" title=\"3.\u8a00\u5f52\u6b63\u4f20\">3.\u8a00\u5f52\u6b63\u4f20<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-14\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#4%E9%94%99%E8%AF%AF%E7%9A%84%E7%90%86%E8%A7%A3Handler\" title=\"4.\u9519\u8bef\u7684\u7406\u89e3Handler\">4.\u9519\u8bef\u7684\u7406\u89e3Handler<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-15\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=4552\/#%E9%AA%8C%E8%AF%81%E5%B8%A6%E5%8F%82Handler\" title=\"\u9a8c\u8bc1\u5e26\u53c2Handler\">\u9a8c\u8bc1\u5e26\u53c2Handler<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"Google%E6%BA%90%E7%A0%81%E5%AF%B9Handler%E7%9A%84%E5%AE%9A%E4%B9%89\"><\/span>Google\u6e90\u7801\u5bf9Handler\u7684\u5b9a\u4e49<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre><code>    \u770bHandler\u6e90\u7801\u5f00\u5173\u6ce8\u91ca\uff1a\n\n    Handler\u5141\u8bb8\u4f60\u53d1\u9001\u548c\u5904\u7406\u548c\u7ebf\u7a0b\u7684MessageQueue\u76f8\u5173\u7684Message\u548cRunnable\u5bf9\u8c61\uff0c\u6bcf\u4e00\u4e2aHandler\u5bf9\u8c61\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5355\u4e00\u7684\u7ebf\u7a0b\u3002<\/code><\/pre>\n<p>\u200b       <\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E4%B8%80%E5%85%88%E7%9C%8B%E7%9C%8B%E6%88%91%E4%BB%AC%E5%B9%B3%E6%97%B6%E6%80%8E%E4%B9%88%E4%BD%BF%E7%94%A8Handler\"><\/span>\u4e00.\u5148\u770b\u770b\u6211\u4eec\u5e73\u65f6\u600e\u4e48\u4f7f\u7528Handler<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre><code>   \u5e73\u65f6\u6211\u4eecHandler\u7528\u7684\u6700\u591a\u7684\u5c31\u662f\u5b50\u7ebf\u7a0b\u5237\u65b0UI,\u5728Activity\u91ccnew\u4e00\u4e2aHandler,\u5b9e\u73b0handleMessage\u65b9\u6cd5,\u7136\u540e\u5728\u5b50\u7ebf\u7a0b\u91cc\u8c03\u7528handler\u5b9e\u4f8b\u53d1\u9001\u6d88\u606f,handleMessage\u5904\u7406\u6d88\u606f,\u5237\u65b0UI.<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E9%82%A3%E6%80%8E%E4%B9%88Handler%E5%B0%B1%E8%83%BD%E5%AE%9E%E7%8E%B0%E5%88%B7%E6%96%B0UI%E7%9A%84%E5%91%A2\"><\/span>\u4e8c.\u90a3\u600e\u4e48Handler\u5c31\u80fd\u5b9e\u73b0\u5237\u65b0UI\u7684\u5462<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre><code>         \u5c31\u662f,\u600e\u4e48\u5c31\u4ece\u5b50\u7ebf\u7a0b\u5207\u5230\u4e3b\u7ebf\u7a0b\u4e86\u5462?\u5efa\u8bae\u5927\u5bb6\u6709\u7a7a\u770b\u770b\u64cd\u4f5c\u7cfb\u7edf\u7684\u5165\u95e8\u4e66\u7c4d,\u7406\u89e3\u4e0b\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u7684\u6982\u5ff5.(\u6211\u7ed9\u5927\u5bb6\u63a8\u8350\u4e00\u672c\u5427<<\u64cd\u4f5c\u7cfb\u7edf\u771f\u8c61\u8fd8\u539f>>,\u7b2c9\u7ae0,\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u7684\u6982\u5ff5\u548c\u533a\u522b)<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"1Handler%E5%88%9B%E5%BB%BA\"><\/span>1.Handler\u521b\u5efa<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"1%EF%BC%89%E9%80%9A%E8%BF%87%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E5%88%9B%E5%BB%BA\"><\/span>1\uff09\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u521b\u5efa<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code>\u7b2c1\u79cd\uff0c\u4e0d\u5e26Looper\u53c2\u6570\u3002\n     public Handler(Callback callback, boolean async)     \n\n \u7b2c2\u79cd\uff0c\u5e26Looper\u53c2\u6570\u3002\n    public Handler(Looper looper, Callback callback, boolean async)<\/code><\/pre>\n<p>\u6ce8\u610f\u8fd9\u4e2aCallback\u7684\u5b9e\u73b0\u65b9\u6cd5handleMessage\u7684\u8fd4\u56de\u503c\u4e3atrue,\u5219Handler\u7684handleMessage\u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u770b\u4e0b\u9762\u7684\u8fd9\u4e2a\u6e90\u7801\u5c31\u80fd\u660e\u767d\uff1a<\/p>\n<pre><code>    \/**\n     * Handle system messages here.\n     *\/\n    public void dispatchMessage(Message msg) {\n        if (msg.callback != null) {\n            handleCallback(msg);\n        } else {\n            if (mCallback != null) {\n                if (mCallback.handleMessage(msg)) {        \/\/\u8fd9\u91cc\n                    return;\n                }\n            }\n            handleMessage(msg);\n        }\n    }<\/code><\/pre>\n<ul>\n<li>\n<h4><span class=\"ez-toc-section\" id=\"%E5%AD%90%E7%BA%BF%E7%A8%8B%E4%B8%AD%E5%88%9B%E5%BB%BAHandler\"><\/span>\u5b50\u7ebf\u7a0b\u4e2d\u521b\u5efaHandler<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5728\u5b50\u7ebf\u7a0b\u4e2d\uff0c\u4e0d\u53ef\u76f4\u63a5\u8c03\u7528Handler\u7684\u6784\u9020\u51fd\u6570\u521b\u5efaHandler\u5bf9\u8c61\uff0c\u5426\u5219\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code>Can&#039;t create handler inside thread  xxx that has not called Looper.prepare()<\/code><\/pre>\n<p>\u770b\u6e90\u7801\u53ef\u4ee5\u77e5\u9053Handler\u7684\u6784\u9020\u65b9\u6cd5\u83b7\u53d6\u7684Looper\u4e3a\u7a7a\u5c31\u4f1a\u62a5\u8fd9\u4e2a\u9519<\/p>\n<pre><code>mLooper = Looper.myLooper();\n    if (mLooper == null) {\n        throw new RuntimeException(\n            &quot;Can&#039;t create handler inside thread &quot; + Thread.currentThread()\n                    + &quot; that has not called Looper.prepare()&quot;);\n    }<\/code><\/pre>\n<p>\u90a3\u4e48\u7591\u95ee\u6765\u4e86\uff0c\u6211\u4eec\u5e73\u65f6\u4f7f\u7528Handler\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u5c31\u662f\u5728Activity\u91ccnew Handler\uff0c\u4e5f\u6ca1\u6709\u4ec0\u4e48\u8bbe\u7f6eLooper\u7684\u64cd\u4f5c\uff0c\u4e3a\u5565\u5c31\u6ca1\u6709\u62a5\u9519\u5462\uff1f\u8bf7\u5f80\u4e0b\u770b\u3002<\/p>\n<\/li>\n<li>\n<h4><span class=\"ez-toc-section\" id=\"%E5%9C%A8Activity%E9%87%8C%E7%9B%B4%E6%8E%A5%E5%88%9B%E5%BB%BAHandler\"><\/span>\u5728Activity\u91cc\u76f4\u63a5\u521b\u5efaHandler<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u770bLooper.myLooper()\u65b9\u6cd5\uff0c\u5b98\u65b9\u7684\u89e3\u91ca\u5c31\u662f\u83b7\u53d6\u548c\u5f53\u524d\u7ebf\u7a0b\u5173\u8054\u7684Looper\u3002<\/p>\n<pre><code>\/**\n * Return the Looper object associated with the current thread.  Returns\n * null if the calling thread is not associated with a Looper.\n *\/\npublic static @Nullable Looper myLooper() {\n    return sThreadLocal.get();\n}<\/code><\/pre>\n<p>\u53ea\u6709Looper.prepare()\u65b9\u6cd5\u88ab\u8c03\u7528\u4e86\uff0csThreadLocal\u624d\u4f1a\u6709\u503c\u3002<\/p>\n<pre><code>\/\/ sThreadLocal.get() will return null unless you&#039;ve called prepare().\nstatic final ThreadLocal&lt;Looper&gt; sThreadLocal = new ThreadLocal&lt;Looper&gt;();<\/code><\/pre>\n<p>\u90a3\u4e48\u5c31\u627e\u5230\u4e00\u4e2a\u7ebf\u7d22\u4e86\uff0c\u4e3b\u7ebf\u7a0b\u80af\u5b9a\u67d0\u4e2a\u5730\u65b9\u8c03\u7528\u4e86Looper.prepare()\uff0cso where is it?<\/p>\n<\/li>\n<\/ul>\n<p>\u00a0\u00a0   <strong>\u5f3a\u70c8\u5efa\u8bae\u8be6\u7ec6\uff08\u6ef4\u6c34\u4e0d\u6f0f\uff09\u770b\u4e00\u904dThreadLocal\u548cLooper\u7684\u6e90\u7801\uff0c\u7b54\u6848\u81ea\u7136\u5c31\u63ed\u6653\u4e86\u3002<\/strong><\/p>\n<p><strong>ThreadLocal\u7684\u6e90\u7801\u5206\u6790<\/strong><\/p>\n<hr \/>\n<p>ThreadLocal\u7c7b\u5e76\u975e\u662fandroid\u7279\u6709\u7684\u7c7b\uff0c\u800c\u662fJava\u89e3\u51b3\u7ebf\u7a0b\u95ee\u9898\u7684\u4e00\u79cd\u624b\u6bb5\u3002<\/p>\n<ul>\n<li>AtomicInteger \uff08\u539f\u5b50\u81ea\u589e\u64cd\u4f5c\uff0c\u7ebf\u7a0b\u5b89\u5168\uff09<br \/>\n<a href=\"https:\/\/blog.csdn.net\/a260724032\/article\/details\/81940785\">https:\/\/blog.csdn.net\/a260724032\/article\/details\/81940785<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/fanrenxiang\/article\/details\/80623884\">https:\/\/blog.csdn.net\/fanrenxiang\/article\/details\/80623884<\/a><\/li>\n<li>\n<p>ThreadLocal\u5b9a\u4e49<br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/6fc3bba12f38\">https:\/\/www.jianshu.com\/p\/6fc3bba12f38<\/a>    (\u63a8\u8350)<br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/69ae8c213b30\">https:\/\/www.jianshu.com\/p\/69ae8c213b30<\/a><br \/>\n<a href=\"https:\/\/www.imooc.com\/article\/45196\">https:\/\/www.imooc.com\/article\/45196<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/u011860731\/article\/details\/48733073\">https:\/\/blog.csdn.net\/u011860731\/article\/details\/48733073<\/a><\/p>\n<p>\u770b\u4e86\u8fd9\u4e48\u591a\u6587\u7ae0\uff0c\u4e5f\u6ca1\u6709\u5bf9ThreadLocal\u8fd9\u4e2a\u7c7b\u5b8c\u5b8c\u5168\u5168\u5730\u7406\u89e3\u3002\u5927\u81f4\u7684\u610f\u601d\u5c31\u662fThreadLocal\u901a\u8fc7\u53d8\u91cf\u5907\u4efd\u7684\u65b9\u6cd5\u89e3\u51b3\u4e86\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>ThreadLocal\u7c7b\u6e90\u7801\u5206\u6790\uff1a<br \/>\n1)get\u65b9\u6cd5\uff1a\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0bt\uff0c\u7136\u540e\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0bt\u7684ThreadLocalMap\u53d8\u91cf\uff0c\u901a\u8fc7\u4f20\u5165\u5f53\u524dThreadLocal\u53d8\u91cf\u4f5c\u4e3akey\uff0c\u83b7\u53d6value\u3002<\/p>\n<\/blockquote>\n<pre><code>**\u5728Looper\u7c7b\u4e2d\uff0cThreadLocal\u5c31\u8d77\u7740\u4e3a\u7ebf\u7a0b\u5b58\u653e\u8bb0\u5f55Looper\u7684\u4f5c\u7528\u3002**<\/code><\/pre>\n<hr \/>\n<p><strong>Looper\u7684\u6e90\u7801\u5206\u6790<\/strong><\/p>\n<hr \/>\n<ul>\n<li>\n<p>\u5b98\u65b9\u5b9a\u4e49<br \/>\n<strong>\u4e3a\u4e00\u4e2a\u7ebf\u7a0b\u542f\u52a8\u6d88\u606f\u5faa\u73af\uff0c\u7ebf\u7a0b\u9ed8\u8ba4\u662f\u6ca1\u6709\u6d88\u606f\u5faa\u73af\u7684\u3002<\/strong><\/p>\n<\/li>\n<li>\n<p>ActivityThread<br \/>\n<img src=\"http:\/\/xinyiworld.top\/wordpress_it\/wp-content\/uploads\/2021\/09\/18328858-ef13433856dc72d1.png\" alt=\"\" \/><br \/>\n\u6ca1\u6709\u9519\uff0c\u4e3b\u7ebf\u7a0b\u7684Looper\u5c31\u662f\u5728\u6b64\u7ed1\u5b9a\u7684\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u4e3b\u7ebf\u7a0b\u91cc\u53ef\u4ee5\u76f4\u63a5new Handler\u7684\u539f\u56e0\uff0c\u7cfb\u7edf\u5df2\u7ecf\u7ed9\u6211\u4eec\u6267\u884c\u4e86Looper.prepare()\u548cLooper.loop()\u65b9\u6cd5\u3002<\/p>\n<p>\u5982\u679c\u4f60\u52e4\u5b66\u597d\u95ee\uff0c\u90a3\u4e48\uff0c\u8bf7\u770bActivityThread\u8fd9\u4e2a\u7c7b\uff0c\u5b83\u5e76\u4e0d\u662f\u7ee7\u627f\u81eaThread\uff0c\u66f4\u4e0d\u662f\u6211\u4eec\u5e38\u8bf4\u7684\u4e3b\u7ebf\u7a0b(UI\u7ebf\u7a0b)\u3002Android\u4e3b\u7ebf\u7a0b\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\uff0c\u4f60\u771f\u6b63\u660e\u767d\u5417\uff1f<br \/>\nActivityThread\u662f\u5982\u4f55\u542f\u52a8\u7684\uff0c\u53ef\u4ee5\u53c2\u7167\u6211\u7684\u8fd9\u7bc7\u535a\u5ba2\uff1a  (<a href=\"https:\/\/www.jianshu.com\/p\/9fcc9d90ade3\">Android\u6e90\u7801\u4e2a\u4e2a\u51fb\u7834\u4e4bLauncher<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u4ec0\u4e48\u662f\u4e3b\u7ebf\u7a0b<br \/>\n<a href=\"https:\/\/blog.csdn.net\/u011631275\/article\/details\/47337385\">https:\/\/blog.csdn.net\/u011631275\/article\/details\/47337385<\/a><br \/>\n\u5728\u6b64\u6211\u4eec\u9700\u8981\u660e\u767d\u4e3b\u7ebf\u7a0b\u548cThread\u7c7b\u6ca1\u6709\u4ec0\u4e48\u5173\u8054\uff0c\u56e0\u4e3a\u4e00\u65e6\u4f7f\u7528\u4e86Thread\uff0c\u5b83\u5c31\u662f\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u4e86\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u535a\u5ba2\uff0c\u4f5c\u8005\u8bf4\u5230\u4e86\u4e3b\u7ebf\u7a0b\u662f\u5982\u4f55\u542f\u52a8\u7684<br \/>\n<img src=\"http:\/\/xinyiworld.top\/wordpress_it\/wp-content\/uploads\/2021\/09\/18328858-94f60bb7e8f6e201.png\" alt=\"\" \/><br \/>\n\u6240\u4ee5\u8981\u5f04\u61c2\u4e3b\u7ebf\u7a0b\u7684\u542f\u52a8\uff0c\u4f60\u5fc5\u987b\u4e86\u89e3\u64cd\u4f5c\u7cfb\u7edf\u3001Linux\u64cd\u4f5c\u7cfb\u7edf\u3001\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u7684\u6982\u5ff5\uff0c\u4e86\u89e3\u4ece\u624b\u673a\u5f00\u673a\u5230Lancher\u542f\u52a8\u7684\u5168\u90e8\u6d41\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>Looper\u7684\u5176\u5b83\u4f5c\u7528\u573a\u666f<br \/>\n\u6709\u7684\u4ee3\u7801\uff0c\u5b83\u662f\u8981\u4e00\u76f4\u5faa\u73af\u4e0d\u65ad\u5730\u6267\u884c\u7684\u3002\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4e00\u542f\u52a8\uff0c\u5c31\u4f1a\u5f00\u542f\u201c\u6b7b\u201d\u5faa\u73af\uff0c\u76d1\u89c6\u7ba1\u7406\u5404\u4e2a\u8fdb\u7a0b\u3002<br \/>\n\u5728Android\u91cc\uff0c\u5f88\u591a\u5904\u90fd\u4f1a\u7528\u5230Looper\u6d88\u606f\u5faa\u73af\u673a\u5236\uff0c  \u5982SystemServer\u7684run\u65b9\u6cd5\uff0c\u8fd8\u6709Activity\u7684\u5404\u4e2a\u751f\u547d\u5468\u671f\u7684\u65b9\u6cd5\u90fd\u662f\u901a\u8fc7Looper\u673a\u5236\u56de\u8c03\u7684\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u8fd9\u4e48\u8bf4\uff0c<strong>Looper\u673a\u5236\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u5b8c\uff0c\u662fLooper\u7ed9\u4e86\u7ebf\u7a0b\u751f\u547d<\/strong>\u3002   <\/p>\n<ul>\n<li>\n<p>loop\u65b9\u6cd5<br \/>\nLooper\u7684loop\u65b9\u6cd5\u5c31\u662f\u8d1f\u8d23\u4e0d\u65ad\u5730\u4ece\u6d88\u606f\u961f\u5217\u91cc\u53d6\u51fa\u6d88\u606f\uff0c\u7136\u540e\u4ea4\u7ed9Handler\u5206\u53d1\u5904\u7406\u3002<\/p>\n<pre><code>for (;;) {\n        Message msg = queue.next(); \/\/ might block\n        if (msg == null) {\n            \/\/ No message indicates that the message queue is quitting.\n            return;\n        }\n\n        try {\n            msg.target.dispatchMessage(msg);\n            dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;\n        } finally {\n            if (traceTag != 0) {\n                Trace.traceEnd(traceTag);\n            }\n        }\n\uff5d<\/code><\/pre>\n<\/li>\n<li>\n<hr \/>\n<p><strong>Looper.loop\u6b7b\u5faa\u73af\u4e3a\u4ec0\u4e48\u4e0d\u4f1a\u5361\u6b7b\uff1f<\/strong><br \/>\n<a href=\"https:\/\/blog.csdn.net\/cshao888\/article\/details\/78386593\">https:\/\/blog.csdn.net\/cshao888\/article\/details\/78386593<\/a><br \/>\n\u6d89\u53ca\u5230Linux\u5e95\u5c42 pipe\/epoll\u673a\u5236\u77e5\u8bc6<br \/>\n<a href=\"https:\/\/blog.csdn.net\/yangwen123\/article\/details\/14118733\">https:\/\/blog.csdn.net\/yangwen123\/article\/details\/14118733<\/a><br \/>\n\u6240\u4ee5\u8bf4android\u6709\u5e9e\u5927\u7684\u77e5\u8bc6\u6846\u67b6\uff0c\u600e\u4e48\u5b66\u4e5f\u5b66\u4e0d\u5b8c\uff0c\u5b66\u4e86java\u8fd8\u8981\u5b66Linux\u3002<\/p>\n<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"2%EF%BC%89%E9%80%9A%E8%BF%87%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95%E5%88%9B%E5%BB%BA\"><\/span>2\uff09\u901a\u8fc7\u9759\u6001\u65b9\u6cd5\u521b\u5efa<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6ce8\u610f8.0\u4e4b\u524d\u7684Handler\u662f\u6ca1\u6709\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa\u7684\u3002<br \/>\n\u9759\u6001\u65b9\u6cd5\u6700\u7ec8\u8c03\u7528\u7684\u8fd8\u662f\u6784\u9020\u65b9\u6cd5<\/p>\n<pre><code>    \/\/9.0\u5f00\u59cb\u6709\u6b64\u65b9\u6cd5\n @NonNull\n    public static Handler createAsync(@NonNull Looper looper) {\n\n    }\n  \/\/9.0\u5f00\u59cb\u6709\u6b64\u65b9\u6cd5\n    @NonNull\n    public static Handler createAsync(@NonNull Looper looper, @NonNull Callback callback) {\n\n    }\n        \/\/8.0\u5f00\u59cb\u6709\u6b64\u65b9\u6cd5\n    \/** @hide *\/\n    @NonNull\n    public static Handler getMain() {\n\n    }\n   \/\/8.0\u5f00\u59cb\u6709\u6b64\u65b9\u6cd5\n    \/** @hide *\/\n    @NonNull\n    public static Handler mainIfNull(@Nullable Handler handler) {\n\n    }<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"2Handler%E4%BD%BF%E7%94%A8\"><\/span>2.Handler\u4f7f\u7528<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e0a\u9762\u521b\u5efa\u4e86Handler\uff0c\u73b0\u5728\u6211\u4eec\u5c31\u6765\u4f7f\u7528Handler<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1%EF%BC%89%E6%89%A7%E8%A1%8CRunnable\"><\/span>1\uff09\u6267\u884cRunnable<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code>    \/**\n     * Causes the Runnable r to be added to the message queue.\n     * The runnable will be run on the thread to which this handler is \n     * attached. \n     *  \n     * @param r The Runnable that will be executed.\n     * \n     * @return Returns true if the Runnable was successfully placed in to the \n     *         message queue.  Returns false on failure, usually because the\n     *         looper processing the message queue is exiting.\n     *\/\n    public final boolean post(Runnable r)\n    {\n       return  sendMessageDelayed(getPostMessage(r), 0);\n    }<\/code><\/pre>\n<p>\u770b\u770b\u5b98\u65b9\u7684\u6ce8\u91ca\uff0c<strong>\u6267\u884crunnable\u5c31\u662f\u5728Handler\u5173\u8054\u7684\u7ebf\u7a0b\u91cc\u6267\u884crunnable\u3002<\/strong><\/p>\n<p>\u53ef\u4ee5\u770b\u5230postXXX\u5404\u79cd\u65b9\u6cd5\uff0c\u6700\u7ec8\u8fd8\u662f\u901a\u8fc7getPostMessage\u65b9\u6cd5\u5c06Runnable\u5c01\u88c5\u5728Message\u91cc<\/p>\n<pre><code>    private static Message getPostMessage(Runnable r) {\n        Message m = Message.obtain();\n        m.callback = r;\n        return m;\n    }<\/code><\/pre>\n<p>\u7136\u540e\u6267\u884csendMessageXXX\u65b9\u6cd5\uff0c\u6700\u7ec8\u4e5f\u5c31\u662f\u6267\u884csendMessageAtTime\u65b9\u6cd5\u3002<\/p>\n<p>post\u4e0esendMessage\u7684\u533a\u522b\u5c31\u5728\u4e8e<strong>post\uff0cHandler\u7684handleMessage\u662f\u6ca1\u6709\u56de\u8c03\u7684<\/strong>\uff0c\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u6ca1\u6709\u56de\u8c03\u4e0b\u9762\u4f1a\u8bb2\u5230\u3002<\/p>\n<pre><code>    \/**\n     * Handle system messages here.\n     *\/\n    public void dispatchMessage(Message msg) {\n        if (msg.callback != null) {\n            handleCallback(msg);\n        } else {\n            if (mCallback != null) {\n                if (mCallback.handleMessage(msg)) {        \/\/\u8fd9\u91cc\n                    return;\n                }\n            }\n            handleMessage(msg);\n        }\n    }<\/code><\/pre>\n<pre><code>    private static void handleCallback(Message message) {\n        message.callback.run();\n    }<\/code><\/pre>\n<p>\u53ef\u89c1Handler\u7684post\u65b9\u6cd5\uff0c\u53ea\u662f\u6267\u884c\u4e86\u4e00\u4e0bRunnable\u7684run\u65b9\u6cd5\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2%EF%BC%89%E5%8F%91%E9%80%81Message\"><\/span>2\uff09\u53d1\u9001Message<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u4e0b\u9762\u770bsendMessageAtTime\u65b9\u6cd5\u7684\u6e90\u7801<\/p>\n<pre><code> public boolean sendMessageAtTime(Message msg, long uptimeMillis) {\n        MessageQueue queue = mQueue;\n        if (queue == null) {\n            RuntimeException e = new RuntimeException(\n                    this + \" sendMessageAtTime() called with no mQueue\");\n            Log.w(\"Looper\", e.getMessage(), e);\n            return false;\n        }\n        return enqueueMessage(queue, msg, uptimeMillis);\n    }<\/code><\/pre>\n<p>\u8fd9\u4e2aqueue\u662f\u5728Handler\u521b\u5efa\u7684\u65f6\u5019\uff0c\u901a\u8fc7Looper\u83b7\u53d6\u7684,\u5728Looper\u7684\u6784\u9020\u65b9\u6cd5\u91cc\u5b9e\u4f8b\u5316\u7684\uff1a<br \/>\n<img src=\"http:\/\/xinyiworld.top\/wordpress_it\/wp-content\/uploads\/2021\/09\/18328858-ad0276e91412b583.png\" alt=\"\" \/><\/p>\n<pre><code>  private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {\n        msg.target = this;\n        if (mAsynchronous) {\n            msg.setAsynchronous(true);\n        }\n        return queue.enqueueMessage(msg, uptimeMillis);\n    }<\/code><\/pre>\n<p>msg.target = this\u5c31\u662f\u8868\u793a\u6d88\u606f\u8981\u7ed9\u9001\u7ed9\u8fd9\u4e2aHandler\u3002<br \/>\n\u4e0b\u9762\u770b\u770bqueue.enqueueMessage\u8fd9\u4e2a\u65b9\u6cd5<\/p>\n<h4><span class=\"ez-toc-section\" id=\"MessageQueue%EF%BC%88%E9%9A%BE%E7%82%B9%EF%BC%89\"><\/span>MessageQueue\uff08\u96be\u70b9\uff09<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><a href=\"https:\/\/blog.csdn.net\/u013749540\/article\/details\/79403851\">https:\/\/blog.csdn.net\/u013749540\/article\/details\/79403851<\/a> \uff08\u8d34\u4e86\u4e0d\u5c11\u6e90\u7801\uff0c\u5f88\u96be\u770b\u61c2\uff09<\/p>\n<ul>\n<li>\u5b98\u65b9\u5b9a\u4e49<br \/>\n\u6301\u6709\u7531Looper\u5206\u53d1\u7684\u6d88\u606f\u5217\u8868\u7684\u4f4e\u7ea7\u7c7b\uff0c\u6d88\u606f\u5e76\u4e0d\u662f\u76f4\u63a5\u6dfb\u52a0\u5230MessageQueue\uff0c\u800c\u662f\u901a\u8fc7\u548cLooper\u76f8\u5173\u8054\u7684Handler\u3002<\/li>\n<li>enqueueMessage\u65b9\u6cd5 \uff08\u5c06\u6d88\u606f\u52a0\u5165\u6d88\u606f\u961f\u5217\uff09<br \/>\n1\uff09\u5982\u679c\u548cHandler\u7ed1\u5b9a\u7684\u7ebf\u7a0b\u6b7b\u4e86\uff0c\u8fd8\u53bb\u8c03\u7528Handler\u53d1\u9001\u6d88\u606f\u5c31\u4f1a\u62a5\u8fd9\u4e2a\u9519\u8bef\u3002<br \/>\n<img src=\"http:\/\/xinyiworld.top\/wordpress_it\/wp-content\/uploads\/2021\/09\/18328858-31c4d61d36b3e147.png\" alt=\"\" \/><br \/>\n2\uff09<\/li>\n<li>next\u65b9\u6cd5<br \/>\n\u7528\u5230\u4e86\u5f88\u591a\u7684Native\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"3%E8%A8%80%E5%BD%92%E6%AD%A3%E4%BC%A0\"><\/span>3.\u8a00\u5f52\u6b63\u4f20<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e0a\u9762\u7814\u7a76\u4e86\u8fd9\u4e48\u591a\uff0c\u5df2\u7ecf\u8bb2\u660e\u767d\u4e86Handler\u662f\u5982\u4f55\u53d1\u9001\u6d88\u606f\u53c8\u662f\u5982\u4f55\u63a5\u53d7\u7684\u6d88\u606f-<strong>\u6d88\u606f\u5faa\u73af\u673a\u5236<\/strong>\u3002<\/p>\n<ul>\n<li><strong>Handler\u600e\u4e48\u5c31\u628a\u7ebf\u7a0b\u8de8\u4e86\u5462\uff1f<\/strong><br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/465329c76770\">https:\/\/www.jianshu.com\/p\/465329c76770<\/a> (\u8a00\u7b80\u610f\u8d45\u7684\u8bf4\u660e\u95ee\u9898)<br \/>\n\u7ebf\u7a0b\u3001Looper\u3001Handler\u4e09\u8005\u7ed1\u5b9a\uff0clooper\u7684loop()\u65b9\u6cd5\u4e00\u76f4\u5728\u7ed1\u5b9a\u7684\u8fd9\u4e2a\u7ebf\u7a0b\u91cc\u8fd0\u884c\uff0c\u5e76\u4e14\u89e3\u6790\u6267\u884cMessage\uff0c\u65e0\u8bba\u4f60Handler\u662f\u5728\u54ea\u4e2a\u7ebf\u7a0b\u53d1\u9001\u6d88\u606f\uff0chandleMessage\u59cb\u7ec8\u5728\u7ed1\u5b9a\u7684\u7ebf\u7a0b\u91cc\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u8bb0\u4f4f\u8fd9\u4e2a\u5b9a\u5f8b\uff1a<\/p>\n<blockquote>\n<p><strong>1)\u51b3\u5b9a\u8de8\u7ebf\u7a0b\u7684\u662fLooper\u7c7b\uff0c\u53ea\u6709Looper\u4e0e\u7ebf\u7a0b\u76f8\u5173\u3002<\/strong><br \/>\n<strong>2)Handler\u7c7b\u53ea\u662f\u6700\u9876\u5c42\u7684\u4e00\u4e2a\u63a5\u53e3\uff0c\u8d1f\u8d23\u4e0eLooper\u7684\u6d88\u606f\u961f\u5217\u4ea4\u4e92\u3002Handler\u4e0e\u7ebf\u7a0b\u65e0\u5173<\/strong><br \/>\n<img src=\"http:\/\/xinyiworld.top\/wordpress_it\/wp-content\/uploads\/2021\/09\/18328858-bdaac3701921a89d.png\" alt=\"\" \/><\/p>\n<\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"4%E9%94%99%E8%AF%AF%E7%9A%84%E7%90%86%E8%A7%A3Handler\"><\/span>4.\u9519\u8bef\u7684\u7406\u89e3Handler<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e00\u822cHandler\u5728Activity\uff08\u4e3b\u7ebf\u7a0b\uff09\u91cc\u76f4\u63a5new\uff0c\u5728\u5b50\u7ebf\u7a0b\u91cc\u8981\u5f00\u542fLooper.prepare\uff0c\u7136\u540e\u518dnew Handler\u3002<br \/>\n\u6240\u4ee5\u4e0d\u77e5\u4e0d\u89c9\u6211\u5c31\u5f62\u6210\u4e86\u4e00\u79cd\u9519\u89c9\uff1a<del>Handler\u5728\u54ea\u4e2a\u7ebf\u7a0b\u91ccnew\u7684\uff0c\u5b83\u7684\u6d88\u606f\u5c31\u4f1a\u5206\u53d1\u5230\u54ea\u4e2a\u7ebf\u7a0b\u3002<\/del><br \/>\n\u5e26\u7740\u8fd9\u4e2a\u201c\u9519\u89c9\u201d\uff0c\u7136\u540e\u53bb\u4e86\u5c0f\u7c73\u9762\u8bd5\uff0c\u9762\u8bd5\u5b98\u5c31\u63d0\u51fa\u4e86\u95ee\u9898\uff1a<strong>\u5982\u679c\u5728\u4e3b\u7ebf\u7a0b\u91cc\u521b\u5efaHandler\uff0c\u7136\u540e\u4f7f\u7528\u5b50\u7ebf\u7a0b\u7684Loopper\u5462\uff1f<\/strong><br \/>\n\u786e\u5b9e\uff0cHandler\u6709\u4e2a\u5e26Looper\u7684\u6784\u9020\u65b9\u6cd5\uff1a<\/p>\n<pre><code>\/**\n     * Use the provided {@link Looper} instead of the default one.\n     * @param looper The looper, must not be null.\n     *\/\n    public Handler(@NonNull Looper looper) {\n        this(looper, null, false);\n    }<\/code><\/pre>\n<p>\u770b\u6ce8\u91ca\u91cc\u201cthe default one\u201d\uff0c\u786e\u5b9a\uff0c\u5982\u679c\u6211new\u65e0\u53c2\u7684Handler\uff0c\u201cthe default one\u201d\u5c31\u662f\u7ebf\u7a0b\u5df2\u7ecf\u7ed1\u5b9a\u597d\u7684Looper\u3002\u6240\u4ee5\u6211\u7684\u9519\u89c9\u201c<del>Handler\u5728\u54ea\u4e2a\u7ebf\u7a0b\u91ccnew\u7684\uff0c\u5b83\u7684\u6d88\u606f\u5c31\u4f1a\u5206\u53d1\u5230\u54ea\u4e2a\u7ebf\u7a0b\u3002<\/del>\u201d\u5982\u679c\u52a0\u4e0a\u4e00\u4e2a\u9650\u5b9a\uff0c\u5c31\u662fnew\u8c03\u7528\u7684\u662fHandler\u7684\u65e0\u53c2\u6784\u9020\u65b9\u6cd5\u5c31\u6ca1\u5565\u6bdb\u75c5\u4e86\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E9%AA%8C%E8%AF%81%E5%B8%A6%E5%8F%82Handler\"><\/span>\u9a8c\u8bc1\u5e26\u53c2Handler<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5b50\u7ebf\u7a0b<\/p>\n<pre><code>public class TestThread extends Thread {\n    private static final String TAG = &quot;TestThread&quot;;\n    private  OnLooperPreparedListener onLooperPreparedListener;\n\n    public TestThread(OnLooperPreparedListener onLooperPreparedListener) {\n        this.onLooperPreparedListener = onLooperPreparedListener;\n    }\n\n    @Override\n    public void run() {\n        super.run();\n        Log.e(TAG, &quot;TestThread name : &quot; + getName());\n        Looper.prepare();\n        if(onLooperPreparedListener != null){\n            onLooperPreparedListener.onLooperPrepare(Looper.myLooper());\n        }\n        Looper.loop();\n    }\n\n    public interface  OnLooperPreparedListener{\n        void onLooperPrepare(Looper looper);\n    }\n}<\/code><\/pre>\n<p>\u5728activity\u91cc\u8c03\u7528<\/p>\n<pre><code>public class HandlerTestActivity extends AppCompatActivity {\n    private static final String TAG = &quot;HandlerTestActivity&quot;;\n    private class MyCallBack implements   Handler.Callback{\n        @Override\n        public boolean handleMessage(@NonNull Message msg) {\n            Log.e(TAG, &quot;handleMessage: &quot; + msg.what + &quot;,&quot; + Thread.currentThread().getName());\n            return false;\n        }\n    }\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_handler_test);\n\n        TestThread thread = new TestThread(new TestThread.OnLooperPreparedListener() {\n            @Override\n            public void onLooperPrepare(Looper looper) {\n                Handler handler = new Handler(looper,new MyCallBack());\n                handler.sendEmptyMessage(0);\n            }\n        });\n        thread.start();\n    }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u65e5\u5fd7<\/p>\n<pre><code>TestThread: TestThread name : Thread-2766\nHandlerTestActivity: handleMessage: 0,Thread-2766<\/code><\/pre>\n<p>Handler\u7684\u5904\u7406\u786e\u5b9e\u662f\u5728\u5b50\u7ebf\u7a0b\u3002<br \/>\n\u597d\u5427\uff0c\u5230\u8fd9\u91cc\u81ea\u5df1\u6253\u81ea\u5df1\u8138\u4e86\u3002\u6240\u4ee5\uff0c<strong>\u79d1\u5b66\u8981\u4e25\u8c28\uff0c\u8981\u591a\u505a\u9a8c\u8bc1\u6d4b\u8bd5\u3002<\/strong><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"4552\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u5728\u9762\u8bd5\u7684\u8fc7\u7a0b\u4e2d,\u53cd\u590d\u7684\u88ab\u95ee\u5230Handler\u673a\u5236,\u5728\u6b64\u5bf9\u5176\u505a\u4e00\u4e2a\u6df1\u5165\u7684\u5256\u6790.\u4e0b\u9762Handler\u7684\u6e90\u7801\u90fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[212],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4552"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4552"}],"version-history":[{"count":9,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4552\/revisions"}],"predecessor-version":[{"id":15436,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/4552\/revisions\/15436"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4552"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}