{"id":270,"date":"2021-07-21T15:12:02","date_gmt":"2021-07-21T07:12:02","guid":{"rendered":"https:\/\/blogs.gongqilin.cn\/?p=270"},"modified":"2021-07-21T15:14:43","modified_gmt":"2021-07-21T07:14:43","slug":"eventbus%e5%88%9d%e6%ad%a5%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/blogs.gongqilin.cn\/?p=270","title":{"rendered":"EventBus\u521d\u6b65\u4f7f\u7528"},"content":{"rendered":"<p>\u8fd9\u662f<strong><a href=\"https:\/\/github.com\/greenrobot\/EventBus\">GitHub\u5730\u5740<\/a>\uff0c<\/strong>\u5728\u6211\u5199\u8fd9\u7bc7\u535a\u5ba2\u4e4b\u524dEventBus\u5df2\u7ecf\u66f4\u65b0\u52303.2\u4e86\uff0c\u6211\u4eec\u5b66\u4e60\u4f7f\u7528\u7684\u662f3.0\u7248\u3002EventBus\u5e93\u4e2d\u6700\u91cd\u8981\u7684\u4e09\u4e2a\u70b9\uff0c\u5206\u522b\u662fsubscriber\uff08\u8ba2\u9605\u8005\uff09\uff0c\u4e8b\u4ef6\uff08\u6d88\u606f\uff09\uff0cpublisher\uff08\u53d1\u5e03\u8005\uff09\u3002<br \/>\nsubscriber \u2014\u2014&gt; EventBus \u7684register\u65b9\u6cd5\uff0c\u4f20\u5165\u7684Activity\u5b9e\u4f8b\u8fdb\u884c\u6ce8\u518c\uff0c\u6307\u660e\u8be5Activity\u53ef\u4ee5\u63a5\u53d7EventBus\u53d1\u5e03\u7684\u4e8b\u4ef6<br \/>\n\u4e8b\u4ef6\uff08Event\uff09\u2014\u2014&gt; EventBus \u7684post\u65b9\u6cd5\uff0c\u53d1\u5e03\u7684\u4e8b\u4ef6\uff0c<br \/>\npublisher\uff08\u53d1\u5e03\u8005\uff09\u2014\u2014&gt; EventBus\u7684post\u65b9\u6cd5\u3002\u53ef\u4ee5\u8ba9\u6ce8\u518c\u4e86\u7684Activity\u6536\u5230\u53d1\u5e03\u7684\u6d88\u606f<\/p>\n<p><span style=\"font-size: 18pt;\">\u7b2c\u4e00\u6b65\uff1a\u6dfb\u52a0\u4f9d\u8d56\u5e93<\/span><\/p>\n<pre class=\"prettyprint linenums\" >implementation 'org.greenrobot:eventbus:3.0.0'<\/pre>\n<p><span style=\"font-size: 18pt;\">\u7b2c\u4e8c\u6b65\uff1a\u6ce8\u518c\u4e0e\u6ce8\u9500<\/span><\/p>\n<p>\u8fd9\u91cc\u6309\u7167\u5b98\u65b9\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u5728onStart()\u6ce8\u518c\uff0c\u5728onStop()\u6ce8\u9500,\u5982\u4e0b<\/p>\n<pre class=\"prettyprint linenums\" > <span class=\"pl-k\">@Override<\/span>\r\n <span class=\"pl-k\">public<\/span> <span class=\"pl-k\">void<\/span> onStart() {\r\n     <span class=\"pl-c1\">super<\/span><span class=\"pl-k\">.<\/span>onStart();\r\n     <span class=\"pl-smi\">EventBus<\/span><span class=\"pl-k\">.<\/span>getDefault()<span class=\"pl-k\">.<\/span>register(<span class=\"pl-c1\">this<\/span>);\r\n }\r\n\r\n <span class=\"pl-k\">@Override<\/span>\r\n <span class=\"pl-k\">public<\/span> <span class=\"pl-k\">void<\/span> onStop() {\r\n     <span class=\"pl-c1\">super<\/span><span class=\"pl-k\">.<\/span>onStop();\r\n     <span class=\"pl-smi\">EventBus<\/span><span class=\"pl-k\">.<\/span>getDefault()<span class=\"pl-k\">.<\/span>unregister(<span class=\"pl-c1\">this<\/span>);\r\n }<\/pre>\n<p><span style=\"font-size: 18pt;\">\u7b2c\u4e09\u6b65\uff1a\u7b80\u5355\u4f7f\u7528<\/span><\/p>\n<p>\u9996\u5148\u6211\u4eec\u5148\u521b\u5efa\u4e00\u4e2a\u6d88\u606f\u7c7b\u578b\u7684\u7c7b<\/p>\n<pre class=\"prettyprint linenums\">public class FirstEvent { \r\n\tprivate String mMsg;\r\n\tpublic FirstEvent(String msg) {\r\n\t\tmMsg = msg;\r\n\t}\r\n\tpublic String getMsg(){\r\n\t\treturn mMsg;\r\n\t}\r\n}<\/pre>\n<p>\u53d1\u9001\u6d88\u606f\u65f6<\/p>\n<pre class=\"prettyprint linenums\">EventBus.getDefault().post(new FirstEvent(&quot;FirstEvent btn clicked&quot;));  \r\n<\/pre>\n<p>\u63a5\u6536\u6d88\u606f\u65f6\uff0c3.0\u7248\u5c31\u4e0e\u4e4b\u524d\u7684\u7248\u672c\u4e0d\u540c\u4e86\uff0c\u524d\u9762\u7684\u7248\u672c\u91c7\u7528<br \/>\nonEvent()<br \/>\nonEventMainThread()<br \/>\nonEventBackgroundThread()<br \/>\nonEventAsync()<br \/>\n\u6765\u5b9e\u73b0\u4e8b\u4ef6\u7684\u63a5\u6536\uff0c\u4f46\u662f3.0\u7248\u672c\u540e\u91c7\u7528\u8ba2\u9605\u4e8b\u4ef6@Subscribe(threadMode = ThreadMode.\u8ba2\u9605\u6a21\u5f0f)\uff0c\u8ba2\u9605\u7684\u6a21\u5f0f\u67e5\u770b\u6e90\u7801\u7684\u6ce8\u91ca<\/p>\n<pre class=\"prettyprint linenums\" >public enum ThreadMode {\r\n    \/**\r\n     * Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery\r\n     * implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for\r\n     * simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers\r\n     * using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.\r\n     *\/\r\n    POSTING,\r\n\r\n    \/**\r\n     * Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is\r\n     * the main thread, event handler methods will be called directly. Event handlers using this mode must return\r\n     * quickly to avoid blocking the main thread.\r\n     *\/\r\n    MAIN,\r\n\r\n    \/**\r\n     * Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods\r\n     * will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single\r\n     * background thread, that will deliver all its events sequentially. Event handlers using this mode should try to\r\n     * return quickly to avoid blocking the background thread.\r\n     *\/\r\n    BACKGROUND,\r\n\r\n    \/**\r\n     * Event handler methods are called in a separate thread. This is always independent from the posting thread and the\r\n     * main thread. Posting events never wait for event handler methods using this mode. Event handler methods should\r\n     * use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number\r\n     * of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus\r\n     * uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.\r\n     *\/\r\n    ASYNC\r\n}<\/pre>\n<p>\u8fd9\u91cc\u7684POSTING\u3001MAIN\u3001BACKGROUND\u3001ASYNC\u4e0e\u4e4b\u524d\u7248\u672c\u7684onEvent()\u3001onEventMainThread()\u3001onEventBackgroundThread()\u3001onEventAsync()\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u3002<br \/>\n<strong>ThreadMode.POSTING<\/strong> \u8ba2\u9605\u8005\u65b9\u6cd5\u5c06\u5728\u53d1\u5e03\u4e8b\u4ef6\u6240\u5728\u7684\u7ebf\u7a0b\u4e2d\u88ab\u8c03\u7528\u3002\u8fd9\u662f \u9ed8\u8ba4\u7684\u7ebf\u7a0b\u6a21\u5f0f\u3002\u4e8b\u4ef6\u7684\u4f20\u9012\u662f\u540c\u6b65\u7684\uff0c\u4e00\u65e6\u53d1\u5e03\u4e8b\u4ef6\uff0c\u6240\u6709\u8be5\u6a21\u5f0f\u7684\u8ba2\u9605\u8005\u65b9\u6cd5\u90fd\u5c06\u88ab\u8c03\u7528\u3002\u8fd9\u79cd\u7ebf\u7a0b\u6a21\u5f0f\u610f\u5473\u7740\u6700\u5c11\u7684\u6027\u80fd\u5f00\u9500\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u7ebf\u7a0b\u7684\u5207\u6362\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u4e0d\u8981\u6c42\u662f\u4e3b\u7ebf\u7a0b\u5e76\u4e14\u8017\u65f6\u5f88\u77ed\u7684\u7b80\u5355\u4efb\u52a1\u63a8\u8350\u4f7f\u7528\u8be5\u6a21\u5f0f\u3002\u4f7f\u7528\u8be5\u6a21\u5f0f\u7684\u8ba2\u9605\u8005\u65b9\u6cd5\u5e94\u8be5\u5feb\u901f\u8fd4\u56de\uff0c\u4ee5\u907f\u514d\u963b\u585e\u53d1\u5e03\u4e8b\u4ef6\u7684\u7ebf\u7a0b\uff0c\u8fd9\u53ef\u80fd\u662f\u4e3b\u7ebf\u7a0b\u3002<br \/>\n<strong>ThreadMode.MAIN<\/strong> \u8ba2\u9605\u8005\u65b9\u6cd5\u5c06\u5728\u4e3b\u7ebf\u7a0b\uff08UI\u7ebf\u7a0b\uff09\u4e2d\u88ab\u8c03\u7528\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u5728\u8be5\u6a21\u5f0f\u7684\u8ba2\u9605\u8005\u65b9\u6cd5\u4e2d\u76f4\u63a5\u66f4\u65b0UI\u754c\u9762\u3002\u5982\u679c\u53d1\u5e03\u4e8b\u4ef6\u7684\u7ebf\u7a0b\u662f\u4e3b\u7ebf\u7a0b\uff0c\u90a3\u4e48\u8be5\u6a21\u5f0f\u7684\u8ba2\u9605\u8005\u65b9\u6cd5\u5c06\u88ab\u76f4\u63a5\u8c03\u7528\u3002\u4f7f\u7528\u8be5\u6a21\u5f0f\u7684\u8ba2\u9605\u8005\u65b9\u6cd5\u5fc5\u987b\u5feb\u901f\u8fd4\u56de\uff0c\u4ee5\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<br \/>\n<strong>ThreadMode.BACKGROUND<\/strong> \u8ba2\u9605\u8005\u65b9\u6cd5\u5c06\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u88ab\u8c03\u7528\u3002\u5982\u679c\u53d1\u5e03\u4e8b\u4ef6\u7684\u7ebf\u7a0b\u4e0d\u662f\u4e3b\u7ebf\u7a0b\uff0c\u90a3\u4e48\u8ba2\u9605\u8005\u65b9\u6cd5\u5c06\u76f4\u63a5\u5728\u8be5\u7ebf\u7a0b\u4e2d\u88ab\u8c03\u7528\u3002\u5982\u679c\u53d1\u5e03\u4e8b\u4ef6\u7684\u7ebf\u7a0b\u662f\u4e3b\u7ebf\u7a0b\uff0c\u90a3\u4e48\u5c06\u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684\u540e\u53f0\u7ebf\u7a0b\uff0c\u8be5\u7ebf\u7a0b\u5c06\u6309\u987a\u5e8f\u53d1\u9001\u6240\u6709\u7684\u4e8b\u4ef6\u3002\u4f7f\u7528\u8be5\u6a21\u5f0f\u7684\u8ba2\u9605\u8005\u65b9\u6cd5\u5e94\u8be5\u5feb\u901f\u8fd4\u56de\uff0c\u4ee5\u907f\u514d\u963b\u585e\u540e\u53f0\u7ebf\u7a0b\u3002<br \/>\n<strong>ThreadMode.ASYNC<\/strong> \u8ba2\u9605\u8005\u65b9\u6cd5\u5c06\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u88ab\u8c03\u7528\u3002\u56e0\u6b64\uff0c\u53d1\u5e03\u4e8b\u4ef6\u7684\u8c03\u7528\u5c06\u7acb\u5373\u8fd4\u56de\u3002\u5982\u679c\u8ba2\u9605\u8005\u65b9\u6cd5\u7684\u6267\u884c\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\uff0c\u4f8b\u5982\u7f51\u7edc\u8bbf\u95ee\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u4f7f\u7528\u8be5\u6a21\u5f0f\u3002\u907f\u514d\u89e6\u53d1\u5927\u91cf\u7684\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u8ba2\u9605\u8005\u65b9\u6cd5\uff0c\u4ee5\u9650\u5236\u5e76\u53d1\u7ebf\u7a0b\u7684\u6570\u91cf\u3002EventBus\u4f7f\u7528\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u6765\u6709\u6548\u5730\u91cd\u7528\u5df2\u7ecf\u5b8c\u6210\u8c03\u7528\u8ba2\u9605\u8005\u65b9\u6cd5\u7684\u7ebf\u7a0b\u3002<\/p>\n<p>\u6240\u4ee5\u5982\u679c\u6211\u4eec\u8fd9\u4e2a\u4e8b\u4ef6\u5904\u7406\u9700\u8981\u66f4\u65b0UI\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<\/p>\n<pre class=\"prettyprint linenums\">\r\n\/\/\u8fd9\u91cc\u6211\u4eec\u91c7\u7528\u7684\u662fThreadMode.MAIN\u8ba2\u9605\u6a21\u5f0f\uff0c\u6839\u636e\u5165\u53c2\u5bf9\u8c61event\u5904\u7406\u4e0d\u540c\u7684\u4e8b\u4ef6\r\n@Subscribe(threadMode = ThreadMode.MainThread)\r\npublic void onEventMainThread(FirstEvent event) {  \r\n  \r\n        \u2026\u2026\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662fGitHub\u5730\u5740\uff0c\u5728\u6211\u5199\u8fd9\u7bc7\u535a\u5ba2\u4e4b\u524dEventBus\u5df2\u7ecf\u66f4\u65b0\u52303.2\u4e86\uff0c\u6211\u4eec\u5b66\u4e60\u4f7f\u7528\u7684\u662f3.0\u7248\u3002Even [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,20],"tags":[],"class_list":["post-270","post","type-post","status-publish","format-standard","hentry","category-andriod","category-20"],"_links":{"self":[{"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=\/wp\/v2\/posts\/270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=270"}],"version-history":[{"count":4,"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=\/wp\/v2\/posts\/270\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=\/wp\/v2\/posts\/270\/revisions\/274"}],"wp:attachment":[{"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gongqilin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}