{"id":1082,"date":"2015-12-23T11:45:36","date_gmt":"2015-12-23T03:45:36","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1082"},"modified":"2026-09-06T16:09:27","modified_gmt":"2026-09-06T08:09:27","slug":"android%e7%9a%84%e6%b6%88%e6%81%af%e5%a4%84%e7%90%86%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1082","title":{"rendered":"android\u7684\u6d88\u606f\u5904\u7406\u673a\u5236"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">android\u7684\u6d88\u606f\u5904\u7406\u6709\u4e09\u4e2a\u6838\u5fc3\u7c7b\uff1aLooper,Handler\u548cMessage\u3002\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2aMessage Queue\uff08\u6d88\u606f\u961f\u5217\uff09\uff0c\u4f46\u662fMQ\u88ab\u5c01\u88c5\u5230Looper\u91cc\u9762\u4e86\uff0c\u6211\u4eec\u4e0d\u4f1a\u76f4\u63a5\u4e0eMQ\u6253\u4ea4\u9053\uff0c\u56e0\u6b64\u6211\u6ca1\u5c06\u5176\u4f5c\u4e3a\u6838\u5fc3\u7c7b\u3002\u4e0b\u9762\u4e00\u4e00\u4ecb\u7ecd\uff1a<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">android\u7684\u6d88\u606f\u5904\u7406\u6709\u4e09\u4e2a\u6838\u5fc3\u7c7b\uff1aLooper,Handler\u548cMessage\u3002\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2aMessage Queue\uff08\u6d88\u606f\u961f\u5217\uff09\uff0c\u4f46\u662fMQ\u88ab\u5c01\u88c5\u5230Looper\u91cc\u9762\u4e86\uff0c\u6211\u4eec\u4e0d\u4f1a\u76f4\u63a5\u4e0eMQ\u6253\u4ea4\u9053\uff0c\u56e0\u6b64\u6211\u6ca1\u5c06\u5176\u4f5c\u4e3a\u6838\u5fc3\u7c7b\u3002\u4e0b\u9762\u4e00\u4e00\u4ecb\u7ecd\uff1a<\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u7ebf\u7a0b\u7684\u9b54\u6cd5\u5e08 Looper<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">Looper\u7684\u5b57\u9762\u610f\u601d\u662f\u201c\u5faa\u73af\u8005\u201d\uff0c\u5b83\u88ab\u8bbe\u8ba1\u7528\u6765\u4f7f\u4e00\u4e2a\u666e\u901a\u7ebf\u7a0b\u53d8\u6210<strong>Looper\u7ebf\u7a0b<\/strong>\u3002\u6240\u8c13Looper\u7ebf\u7a0b\u5c31\u662f\u5faa\u73af\u5de5\u4f5c\u7684\u7ebf\u7a0b\u3002\u5728\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff08\u5c24\u5176\u662fGUI\u5f00\u53d1\u4e2d\uff09\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9700\u8981\u4e00\u4e2a\u7ebf\u7a0b\u4e0d\u65ad\u5faa\u73af\uff0c\u4e00\u65e6\u6709\u65b0\u4efb\u52a1\u5219\u6267\u884c\uff0c\u6267\u884c\u5b8c\u7ee7\u7eed\u7b49\u5f85\u4e0b\u4e00\u4e2a\u4efb\u52a1\uff0c\u8fd9\u5c31\u662fLooper\u7ebf\u7a0b\u3002\u4f7f\u7528Looper\u7c7b\u521b\u5efaLooper\u7ebf\u7a0b\u5f88\u7b80\u5355\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class LooperThread extends Thread {\n\n@Override\npublic void run() {\n        \/\/ \u5c06\u5f53\u524d\u7ebf\u7a0b\u521d\u59cb\u5316\u4e3aLooper\u7ebf\u7a0b\n        Looper.prepare();\n        \/\/ ...\u5176\u4ed6\u5904\u7406\uff0c\u5982\u5b9e\u4f8b\u5316handler\n        \/\/ \u5f00\u59cb\u5faa\u73af\u5904\u7406\u6d88\u606f\u961f\u5217\n        Looper.loop();\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u901a\u8fc7\u4e0a\u9762\u4e24\u884c\u6838\u5fc3\u4ee3\u7801\uff0c\u4f60\u7684\u7ebf\u7a0b\u5c31\u5347\u7ea7\u4e3aLooper\u7ebf\u7a0b\u4e86\uff01\uff01\uff01\u662f\u4e0d\u662f\u5f88\u795e\u5947\uff1f\u8ba9\u6211\u4eec\u653e\u6162\u955c\u5934\uff0c\u770b\u770b\u8fd9\u4e24\u884c\u4ee3\u7801\u5404\u81ea\u505a\u4e86\u4ec0\u4e48\u3002<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">1)Looper.prepare()<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091315284989.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3963\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091315284989.png\" alt=\"\" width=\"472\" height=\"333\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091315284989.png 875w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091315284989-768x542.png 768w\" sizes=\"(max-width: 472px) 100vw, 472px\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u901a\u8fc7\u4e0a\u56fe\u53ef\u4ee5\u770b\u5230\uff0c\u73b0\u5728\u4f60\u7684\u7ebf\u7a0b\u4e2d\u6709\u4e00\u4e2aLooper\u5bf9\u8c61\uff0c\u5b83\u7684\u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2a\u6d88\u606f\u961f\u5217MQ\u3002\u6ce8\u610f\uff0c<strong>\u4e00\u4e2aThread\u53ea\u80fd\u6709\u4e00\u4e2aLooper\u5bf9\u8c61<\/strong>\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u54b1\u4eec\u6765\u770b\u6e90\u7801\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Looper {\n    \/\/ \u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u7684Looper\u5bf9\u8c61\u5176\u5b9e\u662f\u4e00\u4e2aThreadLocal\uff0c\u5373\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8(TLS)\u5bf9\u8c61\n    private static final ThreadLocal sThreadLocal = new ThreadLocal();\n\n    \/\/ Looper\u5185\u7684\u6d88\u606f\u961f\u5217\n    final MessageQueue mQueue;\n\n    \/\/ \u5f53\u524d\u7ebf\u7a0b\n    Thread mThread;\n\n    \/\/ \u3002\u3002\u3002\u5176\u4ed6\u5c5e\u6027\n\n    \/\/ \u6bcf\u4e2aLooper\u5bf9\u8c61\u4e2d\u6709\u5b83\u7684\u6d88\u606f\u961f\u5217\uff0c\u548c\u5b83\u6240\u5c5e\u7684\u7ebf\u7a0b\n    private Looper() {\n        mQueue = new MessageQueue();\n        mRun = true;\n        mThread = Thread.currentThread();\n    }\n\n    \/\/ \u6211\u4eec\u8c03\u7528\u8be5\u65b9\u6cd5\u4f1a\u5728\u8c03\u7528\u7ebf\u7a0b\u7684TLS\u4e2d\u521b\u5efaLooper\u5bf9\u8c61\n    public static final void prepare() {\n        if (sThreadLocal.get() != null) {\n            \/\/ \u8bd5\u56fe\u5728\u6709Looper\u7684\u7ebf\u7a0b\u4e2d\u518d\u6b21\u521b\u5efaLooper\u5c06\u629b\u51fa\u5f02\u5e38\n            throw new RuntimeException(\"Only one Looper may be created per thread\");\n        }\n        sThreadLocal.set(new Looper());\n    }\n    \/\/ \u5176\u4ed6\u65b9\u6cd5\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u901a\u8fc7\u6e90\u7801\uff0cprepare()\u80cc\u540e\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e00\u76ee\u4e86\u7136\uff0c\u5176\u6838\u5fc3\u5c31\u662f\u5c06looper\u5bf9\u8c61\u5b9a\u4e49\u4e3aThreadLocal\u3002\u5982\u679c\u4f60\u8fd8\u4e0d\u6e05\u695a\u4ec0\u4e48\u662fThreadLocal\uff0c\u8bf7\u53c2\u8003<a id=\"url_5\" href=\"http:\/\/blog.csdn.net\/qjyong\/article\/details\/2158097\" target=\"_blank\" rel=\"noopener\">\u300a\u7406\u89e3ThreadLocal\u300b<\/a>\u3002<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">2\uff09Looper.loop()<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091315590586.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3965\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091315590586.png\" alt=\"\" width=\"446\" height=\"507\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u8c03\u7528loop\u65b9\u6cd5\u540e\uff0cLooper\u7ebf\u7a0b\u5c31\u5f00\u59cb\u771f\u6b63\u5de5\u4f5c\u4e86\uff0c\u5b83\u4e0d\u65ad\u4ece\u81ea\u5df1\u7684MQ\u4e2d\u53d6\u51fa\u961f\u5934\u7684\u6d88\u606f(\u4e5f\u53eb\u4efb\u52a1)\u6267\u884c\u3002\u5176\u6e90\u7801\u5206\u6790\u5982\u4e0b\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public static final void loop() {\n        Looper me = myLooper();  \/\/\u5f97\u5230\u5f53\u524d\u7ebf\u7a0bLooper\n        MessageQueue queue = me.mQueue;  \/\/\u5f97\u5230\u5f53\u524dlooper\u7684MQ\n        \/\/ \u8fd9\u4e24\u884c\u6ca1\u770b\u61c2= = \u4e0d\u8fc7\u4e0d\u5f71\u54cd\u7406\u89e3\n        Binder.clearCallingIdentity();\n\n        final long ident = Binder.clearCallingIdentity();\n\n        \/\/ \u5f00\u59cb\u5faa\u73af\n        while (true) {\n            Message msg = queue.next(); \/\/ \u53d6\u51famessage\n            if (msg != null) {\n                if (msg.target == null) {\n                    \/\/ message\u6ca1\u6709target\u4e3a\u7ed3\u675f\u4fe1\u53f7\uff0c\u9000\u51fa\u5faa\u73af\n                    return;\n                }\n                \/\/ \u65e5\u5fd7\u3002\u3002\u3002\n                if (me.mLogging!= null) me.mLogging.println(\n                        \"&gt;&gt;&gt;&gt;&gt; Dispatching to \" + msg.target + \" \"\n                        + msg.callback + \": \" + msg.what\n                        );\n                \/\/ \u975e\u5e38\u91cd\u8981\uff01\u5c06\u771f\u6b63\u7684\u5904\u7406\u5de5\u4f5c\u4ea4\u7ed9message\u7684target\uff0c\u5373\u540e\u9762\u8981\u8bb2\u7684handler\n                msg.target.dispatchMessage(msg);\n                \/\/ \u8fd8\u662f\u65e5\u5fd7\u3002\u3002\u3002\n                if (me.mLogging!= null) me.mLogging.println(\n                        \"&lt;&lt;&lt;&lt;&lt; Finished to    \" + msg.target + \" \"\n                        + msg.callback);\n                                \/\/ \u4e0b\u9762\u6ca1\u770b\u61c2\uff0c\u540c\u6837\u4e0d\u5f71\u54cd\u7406\u89e3\n                final long newIdent = Binder.clearCallingIdentity();\n                if (ident != newIdent) {\n                    Log.wtf(\"Looper\", \"Thread identity changed from 0x\"\n                            + Long.toHexString(ident) + \" to 0x\"\n                            + Long.toHexString(newIdent) + \" while dispatching to \"\n                            + msg.target.getClass().getName() + \" \"+ msg.callback + \" what=\" + msg.what);\n                }\n                \/\/ \u56de\u6536message\u8d44\u6e90\n                msg.recycle();\n            }\n        }\n    }<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u9664\u4e86prepare()\u548cloop()\u65b9\u6cd5\uff0cLooper\u7c7b\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982 Looper.myLooper()\u5f97\u5230\u5f53\u524d\u7ebf\u7a0blooper\u5bf9\u8c61\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public static final Looper myLooper() {\n        \/\/ \u5728\u4efb\u610f\u7ebf\u7a0b\u8c03\u7528Looper.myLooper()\u8fd4\u56de\u7684\u90fd\u662f\u90a3\u4e2a\u7ebf\u7a0b\u7684looper\n        return (Looper)sThreadLocal.get();\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">getThread()\u5f97\u5230looper\u5bf9\u8c61\u6240\u5c5e\u7ebf\u7a0b\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public Thread getThread() {\n        return mThread;\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">quit()\u65b9\u6cd5\u7ed3\u675flooper\u5faa\u73af\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">\u9664\u4e86prepare()\u548cloop()\u65b9\u6cd5\uff0cLooper\u7c7b\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982 Looper.myLooper()\u5f97\u5230\u5f53\u524d\u7ebf\u7a0blooper\u5bf9\u8c61\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public static final Looper myLooper() {\n        \/\/ \u5728\u4efb\u610f\u7ebf\u7a0b\u8c03\u7528Looper.myLooper()\u8fd4\u56de\u7684\u90fd\u662f\u90a3\u4e2a\u7ebf\u7a0b\u7684looper\n        return (Looper)sThreadLocal.get();\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">getThread()\u5f97\u5230looper\u5bf9\u8c61\u6240\u5c5e\u7ebf\u7a0b\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public Thread getThread() {\n        return mThread;\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">quit()\u65b9\u6cd5\u7ed3\u675flooper\u5faa\u73af\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public void quit() {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684message\uff0c\u5b83\u7684target\u4e3aNULL\uff0c\u8868\u793a\u7ed3\u675f\u5faa\u73af\u6d88\u606f\n        Message msg = Message.obtain();\n        \/\/ \u53d1\u51fa\u6d88\u606f\n        mQueue.enqueueMessage(msg, 0);\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5230\u6b64\u4e3a\u6b62\uff0c\u4f60\u5e94\u8be5\u5bf9Looper\u6709\u4e86\u57fa\u672c\u7684\u4e86\u89e3\uff0c\u603b\u7ed3\u51e0\u70b9\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1.\u6bcf\u4e2a\u7ebf\u7a0b\u6709\u4e14\u6700\u591a\u53ea\u80fd\u6709\u4e00\u4e2aLooper\u5bf9\u8c61\uff0c\u5b83\u662f\u4e00\u4e2aThreadLocal<\/p>\n<p style=\"margin: 1em 0px;\">2.Looper\u5185\u90e8\u6709\u4e00\u4e2a\u6d88\u606f\u961f\u5217\uff0cloop()\u65b9\u6cd5\u8c03\u7528\u540e\u7ebf\u7a0b\u5f00\u59cb\u4e0d\u65ad\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6d88\u606f\u6267\u884c<\/p>\n<p style=\"margin: 1em 0px;\">3.Looper\u4f7f\u4e00\u4e2a\u7ebf\u7a0b\u53d8\u6210Looper\u7ebf\u7a0b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u90a3\u4e48\uff0c\u6211\u4eec\u5982\u4f55\u5f80MQ\u4e0a\u6dfb\u52a0\u6d88\u606f\u5462\uff1f\u4e0b\u9762\u6709\u8bf7Handler\uff01<\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u5f02\u6b65\u5904\u7406\u5927\u5e08 Handler<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u4ec0\u4e48\u662fhandler\uff1fhandler\u626e\u6f14\u4e86\u5f80MQ\u4e0a\u6dfb\u52a0\u6d88\u606f\u548c\u5904\u7406\u6d88\u606f\u7684\u89d2\u8272\uff08\u53ea\u5904\u7406\u7531\u81ea\u5df1\u53d1\u51fa\u7684\u6d88\u606f\uff09\uff0c\u5373<strong>\u901a\u77e5MQ\u5b83\u8981\u6267\u884c\u4e00\u4e2a\u4efb\u52a1(sendMessage)\uff0c\u5e76\u5728loop\u5230\u81ea\u5df1\u7684\u65f6\u5019\u6267\u884c\u8be5\u4efb\u52a1(handleMessage)\uff0c\u6574\u4e2a\u8fc7\u7a0b\u662f\u5f02\u6b65\u7684<\/strong>\u3002handler\u521b\u5efa\u65f6\u4f1a\u5173\u8054\u4e00\u4e2alooper\uff0c\u9ed8\u8ba4\u7684\u6784\u9020\u65b9\u6cd5\u5c06\u5173\u8054\u5f53\u524d\u7ebf\u7a0b\u7684looper\uff0c\u4e0d\u8fc7\u8fd9\u4e5f\u662f\u53ef\u4ee5set\u7684\u3002\u9ed8\u8ba4\u7684\u6784\u9020\u65b9\u6cd5\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class handler {\n    final MessageQueue mQueue;  \/\/ \u5173\u8054\u7684MQ\n\n    final Looper mLooper;  \/\/ \u5173\u8054\u7684looper\n\n    final Callback mCallback;\n\n    \/\/ \u5176\u4ed6\u5c5e\u6027\n\n    public Handler() {\n        \/\/ \u6ca1\u770b\u61c2\uff0c\u76f4\u63a5\u7565\u8fc7\uff0c\uff0c\uff0c\n        if (FIND_POTENTIAL_LEAKS) {\n            final Class&lt;? extends Handler&gt; klass = getClass();\n            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &amp;&amp;\n                    (klass.getModifiers() &amp; Modifier.STATIC) == 0) {\n                Log.w(TAG, \"The following Handler class should be static or leaks might occur: \" +\n                    klass.getCanonicalName());\n            }\n        }\n        \/\/ \u9ed8\u8ba4\u5c06\u5173\u8054\u5f53\u524d\u7ebf\u7a0b\u7684looper\n        mLooper = Looper.myLooper();\n        \/\/ looper\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u5373\u8be5\u9ed8\u8ba4\u7684\u6784\u9020\u65b9\u6cd5\u53ea\u80fd\u5728looper\u7ebf\u7a0b\u4e2d\u4f7f\u7528\n        if (mLooper == null) {\n            throw new RuntimeException(\n                \"Can't create handler inside thread that has not called Looper.prepare()\");\n        }\n        \/\/ \u91cd\u8981\uff01\uff01\uff01\u76f4\u63a5\u628a\u5173\u8054looper\u7684MQ\u4f5c\u4e3a\u81ea\u5df1\u7684MQ\uff0c\u56e0\u6b64\u5b83\u7684\u6d88\u606f\u5c06\u53d1\u9001\u5230\u5173\u8054looper\u7684MQ\u4e0a\n        mQueue = mLooper.mQueue;\n        mCallback = null;\n    }\n    \/\/ \u5176\u4ed6\u65b9\u6cd5\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u6211\u4eec\u5c31\u53ef\u4ee5\u4e3a\u4e4b\u524d\u7684LooperThread\u7c7b\u52a0\u5165Handler\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class LooperThread extends Thread {\n    private Handler handler1;\n\n    private Handler handler2;\n\n    @Override\n    public void run() {\n        \/\/ \u5c06\u5f53\u524d\u7ebf\u7a0b\u521d\u59cb\u5316\u4e3aLooper\u7ebf\u7a0b\n        Looper.prepare();\n        \/\/ \u5b9e\u4f8b\u5316\u4e24\u4e2ahandler\n        handler1 = new Handler();\n        handler2 = new Handler();\n        \/\/ \u5f00\u59cb\u5faa\u73af\u5904\u7406\u6d88\u606f\u961f\u5217\n        Looper.loop();\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u52a0\u5165handler\u540e\u7684\u6548\u679c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091322483894.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3966\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091322483894.png\" alt=\"\" width=\"502\" height=\"406\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091322483894.png 950w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091322483894-768x621.png 768w\" sizes=\"(max-width: 502px) 100vw, 502px\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6709\u591a\u4e2aHandler\uff0c\u4f46\u662f\u53ea\u80fd\u6709\u4e00\u4e2aLooper\uff01<\/strong><\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">Handler\u53d1\u9001\u6d88\u606f<\/h3>\n<p style=\"margin: 1em 0px;\">\u6709\u4e86handler\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 <a id=\"url_14\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#post%28java.lang.Runnable%29\" target=\"_blank\" rel=\"noopener\">post(Runnable)<\/a>, \u00a0<a id=\"url_15\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#postAtTime%28java.lang.Runnable,%20long%29\" target=\"_blank\" rel=\"noopener\">postAtTime(Runnable, long)<\/a>, \u00a0<a id=\"url_16\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#postDelayed%28java.lang.Runnable,%20long%29\" target=\"_blank\" rel=\"noopener\">postDelayed(Runnable, long)<\/a>,\u00a0 <a id=\"url_17\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#sendEmptyMessage%28int%29\" target=\"_blank\" rel=\"noopener\">sendEmptyMessage(int)<\/a>, \u00a0<a id=\"url_18\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#sendMessage%28android.os.Message%29\" target=\"_blank\" rel=\"noopener\">sendMessage(Message)<\/a>, \u00a0<a id=\"url_19\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#sendMessageAtTime%28android.os.Message,%20long%29\" target=\"_blank\" rel=\"noopener\">sendMessageAtTime(Message, long)<\/a>\u548c<a id=\"url_20\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#sendMessageDelayed%28android.os.Message,%20long%29\" target=\"_blank\" rel=\"noopener\">sendMessageDelayed(Message, long)<\/a>\u8fd9\u4e9b\u65b9\u6cd5\u5411MQ\u4e0a\u53d1\u9001\u6d88\u606f\u4e86\u3002\u5149\u770b\u8fd9\u4e9bAPI\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97handler\u80fd\u53d1\u4e24\u79cd\u6d88\u606f\uff0c\u4e00\u79cd\u662fRunnable\u5bf9\u8c61\uff0c\u4e00\u79cd\u662fmessage\u5bf9\u8c61\uff0c\u8fd9\u662f\u76f4\u89c2\u7684\u7406\u89e3\uff0c\u4f46\u5176\u5b9epost\u53d1\u51fa\u7684Runnable\u5bf9\u8c61\u6700\u540e\u90fd\u88ab\u5c01\u88c5\u6210message\u5bf9\u8c61\u4e86\uff0c\u89c1\u6e90\u7801\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public final boolean post(Runnable r)\n{\n    \/\/ \u6ce8\u610fgetPostMessage(r)\u5c06runnable\u5c01\u88c5\u6210message\n    return  sendMessageDelayed(getPostMessage(r), 0);\n}\n\nprivate final Message getPostMessage(Runnable r) {\n\n     Message m = Message.obtain();  \/\/\u5f97\u5230\u7a7a\u7684message\n     m.callback = r;  \/\/\u5c06runnable\u8bbe\u4e3amessage\u7684callback\n     return m;\n}\n\npublic boolean sendMessageAtTime(Message msg, long uptimeMillis)\n{\n     boolean sent = false;\n     MessageQueue queue = mQueue;\n     if (queue != null) {\n         msg.target = this;  \/\/ message\u7684target\u5fc5\u987b\u8bbe\u4e3a\u8be5handler\uff01\n         sent = queue.enqueueMessage(msg, uptimeMillis);\n     }\n     else {\n         RuntimeException e = new RuntimeException(\n             this + \" sendMessageAtTime() called with no mQueue\");\n         Log.w(\"Looper\", e.getMessage(), e);\n      }\n      return sent;\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5176\u4ed6\u65b9\u6cd5\u5c31\u4e0d\u7f57\u5217\u4e86\uff0c\u603b\u4e4b\u901a\u8fc7handler\u53d1\u51fa\u7684message\u6709\u5982\u4e0b\u7279\u70b9\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1.message.target\u4e3a\u8be5handler\u5bf9\u8c61\uff0c\u8fd9\u786e\u4fdd\u4e86looper\u6267\u884c\u5230\u8be5message\u65f6\u80fd\u627e\u5230\u5904\u7406\u5b83\u7684handler\uff0c\u5373loop()\u65b9\u6cd5\u4e2d\u7684\u5173\u952e\u4ee3\u7801<\/p>\n<p style=\"margin: 1em 0px;\">msg.target.dispatchMessage(msg);<\/p>\n<p style=\"margin: 1em 0px;\">2.post\u53d1\u51fa\u7684message\uff0c\u5176callback\u4e3aRunnable\u5bf9\u8c61<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">Handler\u5904\u7406\u6d88\u606f<\/h3>\n<p style=\"margin: 1em 0px;\">\u8bf4\u5b8c\u4e86\u6d88\u606f\u7684\u53d1\u9001\uff0c\u518d\u6765\u770b\u4e0bhandler\u5982\u4f55\u5904\u7406\u6d88\u606f\u3002\u6d88\u606f\u7684\u5904\u7406\u662f\u901a\u8fc7\u6838\u5fc3\u65b9\u6cd5<a id=\"url_23\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#dispatchMessage%28android.os.Message%29\" target=\"_blank\" rel=\"noopener\">dispatchMessage<\/a>(<a id=\"url_24\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Message.html\" target=\"_blank\" rel=\"noopener\">Message<\/a> msg)\u4e0e\u94a9\u5b50\u65b9\u6cd5<a id=\"url_25\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#handleMessage%28android.os.Message%29\" target=\"_blank\" rel=\"noopener\">handleMessage<\/a>(<a id=\"url_26\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Message.html\" target=\"_blank\" rel=\"noopener\">Message<\/a>\u00a0msg)\u5b8c\u6210\u7684\uff0c\u89c1\u6e90\u7801<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ \u5904\u7406\u6d88\u606f\uff0c\u8be5\u65b9\u6cd5\u7531looper\u8c03\u7528\npublic void dispatchMessage(Message msg) {\n    if (msg.callback != null) {\n        \/\/ \u5982\u679cmessage\u8bbe\u7f6e\u4e86callback\uff0c\u5373runnable\u6d88\u606f\uff0c\u5904\u7406callback\uff01\n        handleCallback(msg);\n    } else {\n        \/\/ \u5982\u679chandler\u672c\u8eab\u8bbe\u7f6e\u4e86callback\uff0c\u5219\u6267\u884ccallback\n        if (mCallback != null) {\n             \/* \u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u8ba9activity\u7b49\u6765\u5b9e\u73b0Handler.Callback\u63a5\u53e3\uff0c\u907f\u514d\u4e86\u81ea\u5df1\u7f16\u5199handler\u91cd\u5199handleMessage\u65b9\u6cd5\u3002\u89c1http:\/\/alex-yang-xiansoftware-com.iteye.com\/blog\/850865 *\/\n            if (mCallback.handleMessage(msg)) {\n                return;\n            }\n        }\n        \/\/ \u5982\u679cmessage\u6ca1\u6709callback\uff0c\u5219\u8c03\u7528handler\u7684\u94a9\u5b50\u65b9\u6cd5handleMessage\n        handleMessage(msg);\n    }\n}\n\n    \/\/ \u5904\u7406runnable\u6d88\u606f\nprivate final void handleCallback(Message message) {\n    message.callback.run();  \/\/\u76f4\u63a5\u8c03\u7528run\u65b9\u6cd5\uff01\n}\n\n\/\/ \u7531\u5b50\u7c7b\u5b9e\u73b0\u7684\u94a9\u5b50\u65b9\u6cd5\npublic void handleMessage(Message msg) {\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u53ef\u4ee5\u770b\u5230\uff0c\u9664\u4e86<a id=\"url_29\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Handler.html#handleMessage%28android.os.Message%29\" target=\"_blank\" rel=\"noopener\">handleMessage<\/a>(<a id=\"url_30\" href=\"http:\/\/developer.android.com\/reference\/android\/os\/Message.html\" target=\"_blank\" rel=\"noopener\">Message<\/a> msg)\u548cRunnable\u5bf9\u8c61\u7684run\u65b9\u6cd5\u7531\u5f00\u53d1\u8005\u5b9e\u73b0\u5916\uff08\u5b9e\u73b0\u5177\u4f53\u903b\u8f91\uff09\uff0chandler\u7684\u5185\u90e8\u5de5\u4f5c\u673a\u5236\u5bf9\u5f00\u53d1\u8005\u662f\u900f\u660e\u7684\u3002\u8fd9\u6b63\u662fhandler API\u8bbe\u8ba1\u7684\u7cbe\u5999\u4e4b\u5904\uff01<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">Handler\u7684\u7528\u5904<\/h3>\n<p style=\"margin: 1em 0px;\">\u6211\u5728\u5c0f\u6807\u9898\u4e2d\u5c06handler\u63cf\u8ff0\u4e3a\u201c\u5f02\u6b65\u5904\u7406\u5927\u5e08\u201d\uff0c\u8fd9\u5f52\u529f\u4e8eHandler\u62e5\u6709\u4e0b\u9762\u4e24\u4e2a\u91cd\u8981\u7684\u7279\u70b9\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1.handler\u53ef\u4ee5\u5728<strong>\u4efb\u610f\u7ebf\u7a0b\u53d1\u9001\u6d88\u606f<\/strong>\uff0c\u8fd9\u4e9b\u6d88\u606f\u4f1a\u88ab\u6dfb\u52a0\u5230\u5173\u8054\u7684MQ\u4e0a\u3002<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409532564.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3967\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409532564.png\" alt=\"\" width=\"594\" height=\"401\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409532564.png 1376w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409532564-768x519.png 768w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409532564-90x60.png 90w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">2.handler\u662f\u5728\u5b83<strong>\u5173\u8054\u7684looper\u7ebf\u7a0b\u4e2d\u5904\u7406\u6d88\u606f<\/strong>\u7684\u3002<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409541179.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3968\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409541179.png\" alt=\"\" width=\"598\" height=\"436\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409541179.png 1384w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091409541179-768x559.png 768w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u8fd9\u5c31\u89e3\u51b3\u4e86android\u6700\u7ecf\u5178\u7684\u4e0d\u80fd\u5728\u5176\u4ed6\u975e\u4e3b\u7ebf\u7a0b\u4e2d\u66f4\u65b0UI\u7684\u95ee\u9898\u3002<strong>android\u7684\u4e3b\u7ebf\u7a0b\u4e5f\u662f\u4e00\u4e2alooper\u7ebf\u7a0b<\/strong>(looper\u5728android\u4e2d\u8fd0\u7528\u5f88\u5e7f)\uff0c\u6211\u4eec\u5728\u5176\u4e2d\u521b\u5efa\u7684handler\u9ed8\u8ba4\u5c06\u5173\u8054\u4e3b\u7ebf\u7a0bMQ\u3002\u56e0\u6b64\uff0c\u5229\u7528handler\u7684\u4e00\u4e2asolution\u5c31\u662f\u5728activity\u4e2d\u521b\u5efahandler\u5e76\u5c06\u5176\u5f15\u7528\u4f20\u9012\u7ed9worker thread\uff0cworker thread\u6267\u884c\u5b8c\u4efb\u52a1\u540e\u4f7f\u7528handler\u53d1\u9001\u6d88\u606f\u901a\u77e5activity\u66f4\u65b0UI\u3002(\u8fc7\u7a0b\u5982\u56fe)<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091323582123.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3969\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091323582123.png\" alt=\"\" width=\"585\" height=\"554\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091323582123.png 1088w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/12\/2011091323582123-768x728.png 768w\" sizes=\"(max-width: 585px) 100vw, 585px\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u5f53\u7136\uff0chandler\u80fd\u505a\u7684\u8fdc\u8fdc\u4e0d\u4ec5\u5982\u6b64\uff0c\u7531\u4e8e\u5b83\u80fdpost Runnable\u5bf9\u8c61\uff0c\u5b83\u8fd8\u80fd\u4e0eLooper\u914d\u5408\u5b9e\u73b0\u7ecf\u5178\u7684Pipeline Thread(\u6d41\u6c34\u7ebf\u7ebf\u7a0b)\u6a21\u5f0f\u3002\u8bf7\u53c2\u8003\u6b64\u6587<a id=\"url_31\" href=\"http:\/\/mindtherobot.com\/blog\/159\/android-guts-intro-to-loopers-and-handlers\/\" target=\"_blank\" rel=\"noopener\">\u300aAndroid Guts: Intro to Loopers and Handlers\u300b<\/a><\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u5c01\u88c5\u4efb\u52a1 Message<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u5728\u6574\u4e2a\u6d88\u606f\u5904\u7406\u673a\u5236\u4e2d\uff0cmessage\u53c8\u53ebtask\uff0c\u5c01\u88c5\u4e86\u4efb\u52a1\u643a\u5e26\u7684\u4fe1\u606f\u548c\u5904\u7406\u8be5\u4efb\u52a1\u7684handler\u3002message\u7684\u7528\u6cd5\u6bd4\u8f83\u7b80\u5355\uff0c\u8fd9\u91cc\u4e0d\u505a\u603b\u7ed3\u4e86\u3002\u4f46\u662f\u6709\u8fd9\u4e48\u51e0\u70b9\u9700\u8981\u6ce8\u610f\uff08\u5f85\u8865\u5145\uff09\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1.\u5c3d\u7ba1Message\u6709public\u7684\u9ed8\u8ba4\u6784\u9020\u65b9\u6cd5\uff0c\u4f46\u662f\u4f60\u5e94\u8be5\u901a\u8fc7Message.obtain()\u6765\u4ece\u6d88\u606f\u6c60\u4e2d\u83b7\u5f97\u7a7a\u6d88\u606f\u5bf9\u8c61\uff0c\u4ee5\u8282\u7701\u8d44\u6e90\u3002<\/p>\n<p style=\"margin: 1em 0px;\">2.\u5982\u679c\u4f60\u7684message\u53ea\u9700\u8981\u643a\u5e26\u7b80\u5355\u7684int\u4fe1\u606f\uff0c\u8bf7\u4f18\u5148\u4f7f\u7528Message.arg1\u548cMessage.arg2\u6765\u4f20\u9012\u4fe1\u606f\uff0c\u8fd9\u6bd4\u7528Bundle\u66f4\u7701\u5185\u5b58<\/p>\n<p style=\"margin: 1em 0px;\">3.\u64c5\u7528message.what\u6765\u6807\u8bc6\u4fe1\u606f\uff0c\u4ee5\u4fbf\u7528\u4e0d\u540c\u65b9\u5f0f\u5904\u7406message\u3002<\/p>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u5176\u5b83\u7406\u89e3<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">Message Queue \u5728\u5355\u7ebf\u7a0b\u6a21\u578b\u4e0b\uff0c\u4e3a\u4e86\u89e3\u51b3\u7c7b\u4f3c\u7684\u95ee\u9898\uff0cAndroid\u8bbe\u8ba1\u4e86\u4e00\u4e2aMessage Queue(\u6d88\u606f\u961f\u5217)\uff0c \u7ebf\u7a0b\u95f4\u53ef\u4ee5\u901a\u8fc7\u8be5Message Queue\u5e76\u7ed3\u5408Handler\u548cLooper\u7ec4\u4ef6\u8fdb\u884c\u4fe1\u606f\u4ea4\u6362\u3002\u4e0b\u9762\u5c06\u5bf9\u5b83\u4eec\u8fdb\u884c\u5206\u522b\u4ecb\u7ecd\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1. Message<\/p>\n<p style=\"margin: 1em 0px;\">Message\u6d88\u606f\uff0c\u7406\u89e3\u4e3a\u7ebf\u7a0b\u95f4\u4ea4\u6d41\u7684\u4fe1\u606f\uff0c\u5904\u7406\u6570\u636e\u540e\u53f0\u7ebf\u7a0b\u9700\u8981\u66f4\u65b0UI\uff0c\u5219\u53d1\u9001Message\u5185\u542b\u4e00\u4e9b\u6570\u636e\u7ed9UI\u7ebf\u7a0b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">2. Handler<\/p>\n<p style=\"margin: 1em 0px;\">Handler\u5904\u7406\u8005\uff0c\u662fMessage\u7684\u4e3b\u8981\u5904\u7406\u8005\uff0c\u8d1f\u8d23Message\u7684\u53d1\u9001\uff0cMessage\u5185\u5bb9\u7684\u6267\u884c\u5904\u7406\u3002\u540e\u53f0\u7ebf\u7a0b\u5c31\u662f\u901a\u8fc7\u4f20\u8fdb\u6765\u7684Handler\u5bf9\u8c61\u5f15\u7528\u6765sendMessage(Message)\u3002\u800c\u4f7f\u7528Handler\uff0c\u9700\u8981implement \u8be5\u7c7b\u7684 handleMessage(Message)\u65b9\u6cd5\uff0c\u5b83\u662f\u5904\u7406\u8fd9\u4e9bMessage\u7684\u64cd\u4f5c\u5185\u5bb9\uff0c\u4f8b\u5982Update UI\u3002\u901a\u5e38\u9700\u8981\u5b50\u7c7b\u5316Handler\u6765\u5b9e\u73b0handleMessage\u65b9\u6cd5\u3002<\/p>\n<p style=\"margin: 1em 0px;\">3. Message Queue<\/p>\n<p style=\"margin: 1em 0px;\">Message Queue\u6d88\u606f\u961f\u5217\uff0c\u7528\u6765\u5b58\u653e\u901a\u8fc7Handler\u53d1\u5e03\u7684\u6d88\u606f\uff0c\u6309\u7167\u5148\u8fdb\u5148\u51fa\u6267\u884c\u3002 \u6bcf\u4e2amessage queue\u90fd\u4f1a\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684Handler\u3002Handler\u4f1a\u5411message queue\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u53d1\u9001\u6d88\u606f\uff1asendMessage\u6216post\u3002\u8fd9\u4e24\u79cd\u6d88\u606f\u90fd\u4f1a\u63d2\u5728message queue\u961f\u5c3e\u5e76\u6309\u5148\u8fdb\u5148\u51fa\u6267\u884c\u3002\u4f46\u901a\u8fc7\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53d1\u9001\u7684\u6d88\u606f\u6267\u884c\u7684\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\uff1a\u901a\u8fc7sendMessage\u53d1\u9001\u7684\u662f\u4e00\u4e2amessage\u5bf9\u8c61,\u4f1a\u88abHandler\u7684handleMessage()\u51fd\u6570\u5904\u7406\uff1b\u800c\u901a\u8fc7post\u65b9\u6cd5\u53d1\u9001\u7684\u662f\u4e00\u4e2arunnable\u5bf9\u8c61\uff0c\u5219\u4f1a\u81ea\u5df1\u6267\u884c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">4. Looper<\/p>\n<p style=\"margin: 1em 0px;\">Looper\u662f\u6bcf\u6761\u7ebf\u7a0b\u91cc\u7684Message Queue\u7684\u7ba1\u5bb6\u3002Android\u6ca1\u6709Global\u7684Message Queue\uff0c\u800cAndroid\u4f1a\u81ea\u52a8\u66ff\u4e3b\u7ebf\u7a0b(UI\u7ebf\u7a0b)\u5efa\u7acbMessage Queue\uff0c\u4f46\u5728\u5b50\u7ebf\u7a0b\u91cc\u5e76\u6ca1\u6709\u5efa\u7acbMessage Queue\u3002\u6240\u4ee5\u8c03\u7528Looper.getMainLooper()\u5f97\u5230\u7684\u4e3b\u7ebf\u7a0b\u7684Looper\u4e0d\u4e3aNULL\uff0c\u4f46\u8c03\u7528Looper.myLooper()\u5f97\u5230\u5f53\u524d\u7ebf\u7a0b\u7684Looper\u5c31\u6709\u53ef\u80fd\u4e3aNULL\u3002 \u5bf9\u4e8e\u5b50\u7ebf\u7a0b\u4f7f\u7528Looper\uff0cAPI Doc\u63d0\u4f9b\u4e86\u6b63\u786e\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a class LooperThread extends Thread { public Handler mHandler;<\/p>\n<p style=\"margin: 1em 0px;\">public void run() { Looper.prepare(); \/\/\u521b\u5efa\u672c\u7ebf\u7a0b\u7684Looper\u5e76\u521b\u5efa\u4e00\u4e2aMessageQueue<\/p>\n<p style=\"margin: 1em 0px;\">mHandler = new Handler() { public void handleMessage(Message msg) { \/\/ process incoming messages here } };<\/p>\n<p style=\"margin: 1em 0px;\">Looper.loop(); \/\/\u5f00\u59cb\u8fd0\u884cLooper,\u76d1\u542cMessage Queue } }<\/p>\n<p style=\"margin: 1em 0px;\">\u8fd9\u4e2aMessage\u673a\u5236\u7684\u5927\u6982\u6d41\u7a0b\uff1a<\/p>\n<p style=\"margin: 1em 0px;\">1. \u5728Looper.loop()\u65b9\u6cd5\u8fd0\u884c\u5f00\u59cb\u540e\uff0c\u5faa\u73af\u5730\u6309\u7167\u63a5\u6536\u987a\u5e8f\u53d6\u51faMessage Queue\u91cc\u9762\u7684\u975eNULL\u7684Message\u3002<\/p>\n<p style=\"margin: 1em 0px;\">2. \u4e00\u5f00\u59cbMessage Queue\u91cc\u9762\u7684Message\u90fd\u662fNULL\u7684\u3002\u5f53Handler.sendMessage(Message)\u5230Message Queue\uff0c\u8be5\u51fd\u6570\u91cc\u9762\u8bbe\u7f6e\u4e86\u90a3\u4e2aMessage\u5bf9\u8c61\u7684target\u5c5e\u6027\u662f\u5f53\u524d\u7684Handler\u5bf9\u8c61\u3002\u968f\u540eLooper\u53d6\u51fa\u4e86\u90a3\u4e2aMessage\uff0c\u5219\u8c03\u7528\u8be5Message\u7684target\u6307\u5411\u7684Hander\u7684dispatchMessage\u51fd\u6570\u5bf9Message\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5728dispatchMessage\u65b9\u6cd5\u91cc\uff0c\u5982\u4f55\u5904\u7406Message\u5219\u7531\u7528\u6237\u6307\u5b9a\uff0c\u4e09\u4e2a\u5224\u65ad\uff0c\u4f18\u5148\u7ea7\u4ece\u9ad8\u5230\u4f4e\uff1a 1) Message\u91cc\u9762\u7684Callback\uff0c\u4e00\u4e2a\u5b9e\u73b0\u4e86Runnable\u63a5\u53e3\u7684\u5bf9\u8c61\uff0c\u5176\u4e2drun\u51fd\u6570\u505a\u5904\u7406\u5de5\u4f5c\uff1b 2) Handler\u91cc\u9762\u7684mCallback\u6307\u5411\u7684\u4e00\u4e2a\u5b9e\u73b0\u4e86Callback\u63a5\u53e3\u7684\u5bf9\u8c61\uff0c\u7531\u5176handleMessage\u8fdb\u884c\u5904\u7406\uff1b 3) \u5904\u7406\u6d88\u606fHandler\u5bf9\u8c61\u5bf9\u5e94\u7684\u7c7b\u7ee7\u627f\u5e76\u5b9e\u73b0\u4e86\u5176\u4e2dhandleMessage\u51fd\u6570\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5b9e\u73b0\u7684handleMessage\u51fd\u6570\u5904\u7406\u6d88\u606f\u3002 \u7531\u6b64\u53ef\u89c1\uff0c\u6211\u4eec\u5b9e\u73b0\u7684handleMessage\u65b9\u6cd5\u662f\u4f18\u5148\u7ea7\u6700\u4f4e\u7684\uff01<\/p>\n<p style=\"margin: 1em 0px;\">3. Handler\u5904\u7406\u5b8c\u8be5Message (update UI) \u540e\uff0cLooper\u5219\u8bbe\u7f6e\u8be5Message\u4e3aNULL\uff0c\u4ee5\u4fbf\u56de\u6536\uff01<\/p>\n<p style=\"margin: 1em 0px;\">\u539f\u6587\u51fa\u5904:<a id=\"cb_post_title_url\" class=\"postTitle2\" href=\"http:\/\/www.cnblogs.com\/codingmyworld\/archive\/2011\/09\/14\/2174255.html\">android\u7684\u6d88\u606f\u5904\u7406\u673a\u5236\uff08\u56fe+\u6e90\u7801\u5206\u6790\uff09\u2014\u2014Looper,Handler,Message<\/a><\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aAndroid \u6d88\u606f\u673a\u5236\u5c31\u662f Looper \u5faa\u73af\u53d6 MessageQueue\uff0cHandler \u8d1f\u8d23\u53d1\u9001\u4e0e\u5904\u7406\uff0cMessage \u5c01\u88c5\u4efb\u52a1\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>android\u7684\u6d88\u606f\u5904\u7406\u6709\u4e09\u4e2a\u6838\u5fc3\u7c7b\uff1aLooper,Handler\u548cMessage\u3002\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2aMessage [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[196],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1082"}],"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=1082"}],"version-history":[{"count":4,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1082\/revisions"}],"predecessor-version":[{"id":3970,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1082\/revisions\/3970"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}