{"id":1688,"date":"2016-08-30T14:47:04","date_gmt":"2016-08-30T06:47:04","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1688"},"modified":"2020-05-08T20:03:50","modified_gmt":"2020-05-08T12:03:50","slug":"eventbus%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1688","title":{"rendered":"EventBus\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"<h2>\u6982\u8ff0<\/h2>\n<p>EventBus\u662f\u4e00\u6b3e\u9488\u5bf9Android\u4f18\u5316\u7684\u53d1\u5e03\/\u8ba2\u9605\uff08publish\/subscribe\uff09\u4e8b\u4ef6\u603b\u7ebf\u3002\u4e3b\u8981\u529f\u80fd\u662f\u66ff\u4ee3Intent,Handler,BroadCast\u5728Fragment\uff0cActivity\uff0cService\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\u3002\u7b80\u5316\u4e86\u5e94\u7528\u7a0b\u5e8f\u5185\u5404\u7ec4\u4ef6\u95f4\u3001\u7ec4\u4ef6\u4e0e\u540e\u53f0\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u3002\u4f18\u70b9\u662f\u5f00\u9500\u5c0f\uff0c\u4ee3\u7801\u66f4\u4f18\u96c5\u3002\u4ee5\u53ca\u5c06\u53d1\u9001\u8005\u548c\u63a5\u6536\u8005\u89e3\u8026\u3002\u6bd4\u5982\u8bf7\u6c42\u7f51\u7edc\uff0c\u7b49\u7f51\u7edc\u8fd4\u56de\u65f6\u901a\u8fc7Handler\u6216Broadcast\u901a\u77e5UI\uff0c\u4e24\u4e2aFragment\u4e4b\u95f4\u9700\u8981\u901a\u8fc7Listener\u901a\u4fe1\uff0c\u8fd9\u4e9b\u9700\u6c42\u90fd\u53ef\u4ee5\u901a\u8fc7EventBus\u5b9e\u73b0\u3002<img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1689\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/08\/eventbusall.png\" alt=\"eventbusall\" width=\"802\" height=\"396\" \/><\/p>\n<h2>EventBus\u4f5c\u4e3a\u4e00\u4e2a\u6d88\u606f\u603b\u7ebf\uff0c\u6709\u4e09\u4e2a\u4e3b\u8981\u7684\u5143\u7d20\uff1a<\/h2>\n<p><span style=\"color: #993300;\"><strong>Event<\/strong><\/span>\uff1a\u4e8b\u4ef6\u3002\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61<br \/>\n<span style=\"color: #993300;\"><strong>Subscriber<\/strong><\/span>\uff1a\u4e8b\u4ef6\u8ba2\u9605\u8005\uff0c\u63a5\u6536\u7279\u5b9a\u7684\u4e8b\u4ef6\u3002\u5728EventBus\u4e2d\uff0c\u4f7f\u7528\u7ea6\u5b9a\u6765\u6307\u5b9a\u4e8b\u4ef6\u8ba2\u9605\u8005\u4ee5\u7b80\u5316\u4f7f\u7528\u3002\u5373\u6240\u6709\u4e8b\u4ef6\u8ba2\u9605\u90fd\u90fd\u662f\u4ee5onEvent\u5f00\u5934\u7684\u51fd\u6570\uff0c3.0\u4e4b\u540e\u4f7f\u7528@Subscribe \u6ce8\u89e3\u6307\u5b9a\u3002<br \/>\n<span style=\"color: #993300;\"><strong>Publisher<\/strong><\/span>\uff1a\u4e8b\u4ef6\u53d1\u5e03\u8005\uff0c\u7528\u4e8e\u901a\u77e5 Subscriber \u6709\u4e8b\u4ef6\u53d1\u751f\u3002\u53ef\u4ee5\u5728\u4efb\u610f\u7ebf\u7a0b\u4efb\u610f\u4f4d\u7f6e\u53d1\u9001\u4e8b\u4ef6\uff0c\u76f4\u63a5\u8c03\u7528eventBus.post(Object) \u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5df1\u5b9e\u4f8b\u5316 EventBus \u5bf9\u8c61\uff0c\u4f46\u4e00\u822c\u4f7f\u7528\u9ed8\u8ba4\u7684\u5355\u4f8b\u5c31\u597d\u4e86\uff1aEventBus.getDefault()\uff0c \u6839\u636epost\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u4f1a\u81ea\u52a8\u8c03\u7528\u8ba2\u9605\u76f8\u5e94\u7c7b\u578b\u4e8b\u4ef6\u7684\u51fd\u6570\u3002<\/p>\n<h2>\u5173\u4e8eThreadMode<\/h2>\n<p>\u524d\u9762\u8bf4\u4e86\uff0cSubscriber\u7684\u51fd\u6570\u53ea\u80fd\u662f\u90a34\u4e2a\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u4e8b\u4ef6\u8ba2\u9605\u51fd\u6570\u90fd\u662f\u548c\u4e00\u4e2aThreadMode\u76f8\u5173\u8054\u7684\uff0cThreadMode\u6307\u5b9a\u4e86\u4f1a\u8c03\u7528\u7684\u51fd\u6570\u3002\u6709\u4ee5\u4e0b\u56db\u4e2aThreadMode\uff1a<\/p>\n<p><span style=\"color: #993300;\"><strong>PostThread<\/strong><\/span>\uff1a\u4e8b\u4ef6\u7684\u5904\u7406\u5728\u548c\u4e8b\u4ef6\u7684\u53d1\u9001\u5728\u76f8\u540c\u7684\u8fdb\u7a0b\uff0c\u6240\u4ee5\u4e8b\u4ef6\u5904\u7406\u65f6\u95f4\u4e0d\u5e94\u592a\u957f\uff0c\u4e0d\u7136\u5f71\u54cd\u4e8b\u4ef6\u7684\u53d1\u9001\u7ebf\u7a0b\uff0c\u800c\u8fd9\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u662fUI\u7ebf\u7a0b\u3002\u5bf9\u5e94\u7684\u51fd\u6570\u540d\u662fonEvent\u3002<br \/>\n<span style=\"color: #993300;\"><strong>MainThread<\/strong><\/span>: \u4e8b\u4ef6\u7684\u5904\u7406\u4f1a\u5728UI\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002\u4e8b\u4ef6\u5904\u7406\u65f6\u95f4\u4e0d\u80fd\u592a\u957f\uff0c\u8fd9\u4e2a\u4e0d\u7528\u8bf4\u7684\uff0c\u957f\u4e86\u4f1aANR\u7684\uff0c\u5bf9\u5e94\u7684\u51fd\u6570\u540d\u662fonEventMainThread\u3002<br \/>\n<span style=\"color: #993300;\"><strong>BackgroundThread<\/strong><\/span>\uff1a\u4e8b\u4ef6\u7684\u5904\u7406\u4f1a\u5728\u4e00\u4e2a\u540e\u53f0\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u5bf9\u5e94\u7684\u51fd\u6570\u540d\u662fonEventBackgroundThread\uff0c\u867d\u7136\u540d\u5b57\u662fBackgroundThread\uff0c\u4e8b\u4ef6\u5904\u7406\u662f\u5728\u540e\u53f0\u7ebf\u7a0b\uff0c\u4f46\u4e8b\u4ef6\u5904\u7406\u65f6\u95f4\u8fd8\u662f\u4e0d\u5e94\u8be5\u592a\u957f\uff0c\u56e0\u4e3a\u5982\u679c\u53d1\u9001\u4e8b\u4ef6\u7684\u7ebf\u7a0b\u662f\u540e\u53f0\u7ebf\u7a0b\uff0c\u4f1a\u76f4\u63a5\u6267\u884c\u4e8b\u4ef6\uff0c\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u662fUI\u7ebf\u7a0b\uff0c\u4e8b\u4ef6\u4f1a\u88ab\u52a0\u5230\u4e00\u4e2a\u961f\u5217\u4e2d\uff0c\u7531\u4e00\u4e2a\u7ebf\u7a0b\u4f9d\u6b21\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u5982\u679c\u67d0\u4e2a\u4e8b\u4ef6\u5904\u7406\u65f6\u95f4\u592a\u957f\uff0c\u4f1a\u963b\u585e\u540e\u9762\u7684\u4e8b\u4ef6\u7684\u6d3e\u53d1\u6216\u5904\u7406\u3002<br \/>\n<span style=\"color: #993300;\"><strong>Async<\/strong><\/span>\uff1a\u4e8b\u4ef6\u5904\u7406\u4f1a\u5728\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u6267\u884c\u8017\u65f6\u64cd\u4f5c\uff0c\u6bcf\u4e2a\u4e8b\u4ef6\u4f1a\u5f00\u542f\u4e00\u4e2a\u7ebf\u7a0b\uff08\u6709\u7ebf\u7a0b\u6c60\uff09\uff0c\u4f46\u6700\u597d\u9650\u5236\u7ebf\u7a0b\u7684\u6570\u76ee\u3002<br \/>\n\u6839\u636e\u4e8b\u4ef6\u8ba2\u9605\u90fd\u51fd\u6570\u540d\u79f0\u7684\u4e0d\u540c\uff0c\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684ThreadMode\uff0c\u6bd4\u5982\u679c\u5728\u540e\u53f0\u7ebf\u7a0b\u52a0\u8f7d\u4e86\u6570\u636e\u60f3\u5728UI\u7ebf\u7a0b\u663e\u793a\uff0c\u8ba2\u9605\u8005\u53ea\u9700\u628a\u51fd\u6570\u547d\u540donEventMainThread\u3002<\/p>\n<h2>\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u5f15\u5165EventBus:<\/p>\n<p>[java]compile &#8216;de.greenrobot:eventbus:3.0.0-beta1&#8217;<br \/>\n\u5b9a\u4e49\u4e8b\u4ef6:<\/p>\n<p>public class MessageEvent { \/* Additional fields if needed *\/ }<br \/>\n\u6ce8\u518c\u4e8b\u4ef6\u63a5\u6536\u8005\uff1a<\/p>\n<p>eventBus.register(this);<br \/>\n\u53d1\u9001\u4e8b\u4ef6:<\/p>\n<p>eventBus.post(event)<br \/>\n\u63a5\u6536\u6d88\u606f\u5e76\u5904\u7406:<\/p>\n<p>public void onEvent(MessageEvent event) {}<br \/>\n\u6ce8\u9500\u4e8b\u4ef6\u63a5\u6536\uff1a<\/p>\n<p>eventBus.unregister(this);<br \/>\n\u6700\u540e\uff0cproguard \u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u5904\u7406:<\/p>\n<p>#EventBus<br \/>\n -keepclassmembers class ** {<br \/>\n    public void onEvent*(**);<br \/>\n    void onEvent*(**);<br \/>\n }<br \/>\n[\/java]<\/p>\n<h2>\u9ecf\u6027\u4e8b\u4ef6<\/h2>\n<p>EventBus\u8fd8\u652f\u6301\u53d1\u9001\u9ecf\u6027\u4e8b\u4ef6\u3002<\/p>\n<p>\u4f55\u4e3a\u9ecf\u6027\u4e8b\u4ef6\u5462\uff1f<br \/>\n\u7b80\u5355\u8bb2\uff0c\u5c31\u662f\u5728\u53d1\u9001\u4e8b\u4ef6\u4e4b\u540e\u518d\u8ba2\u9605\u8be5\u4e8b\u4ef6\u4e5f\u80fd\u6536\u5230\u8be5\u4e8b\u4ef6\uff0c\u8ddf\u9ecf\u6027\u5e7f\u64ad\u7c7b\u4f3c\u3002<\/p>\n<p>\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>[java]<br \/>\n\u8ba2\u9605\u9ecf\u6027\u4e8b\u4ef6<\/p>\n<p>EventBus.getDefault().register(StickyModeActivity.this);<\/p>\n<p>\u9ecf\u6027\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff1a<\/p>\n<p>   @Subscribe(sticky = true)<br \/>\n   public void XXX(MessageEvent messageEvent) {<br \/>\n    &#8230;&#8230;<br \/>\n   }<\/p>\n<p>\u53d1\u9001\u9ecf\u6027\u4e8b\u4ef6\uff1a<\/p>\n<p>EventBus.getDefault().postSticky(new MessageEvent(&quot;test&quot;));<\/p>\n<p>  \u5904\u7406\u6d88\u606f\u4e8b\u4ef6\u4ee5\u53ca\u53d6\u6d88\u8ba2\u9605\u548c\u4e0a\u9762\u65b9\u5f0f\u76f8\u540c\u3002<br \/>\n[\/java]<\/p>\n<h2>\u4e8b\u4f8b<\/h2>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/08\/event4.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1696\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/08\/event4.png\" alt=\"event4\" width=\"415\" height=\"228\" \/><\/a><\/p>\n<p><a href=\"https:\/\/github.com\/yihu0817\/EventBus_warmtel\">\u4e0b\u8f7d\u70b9\u8fd9\u91cc<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/lijianxinchina.github.io\/2016\/04\/05\/EventBus%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90\/\">EventBus\u6e90\u7801\u89e3\u6790 TmWork<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 EventBus\u662f\u4e00\u6b3e\u9488\u5bf9Android\u4f18\u5316\u7684\u53d1\u5e03\/\u8ba2\u9605\uff08publish\/subscribe\uff09\u4e8b\u4ef6\u603b\u7ebf\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1688"}],"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=1688"}],"version-history":[{"count":9,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1688\/revisions"}],"predecessor-version":[{"id":1699,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1688\/revisions\/1699"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}