{"id":1014,"date":"2017-12-30T16:35:00","date_gmt":"2017-12-30T08:35:00","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=1014"},"modified":"2021-07-22T10:12:53","modified_gmt":"2021-07-22T02:12:53","slug":"android%e7%94%a8eventbus%e5%af%a6%e4%bd%9c%e8%a7%80%e5%af%9f%e8%80%85%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=1014","title":{"rendered":"[Android]\u7528EventBus\u5be6\u4f5c\u89c0\u5bdf\u8005\u6a21\u5f0f"},"content":{"rendered":"\n<p>\u89c0\u5bdf\u8005\u6a21\u5f0f\u662f\u8edf\u9ad4\u8a2d\u8a08\u6a21\u5f0f\u7684\u4e00\u7a2e\uff0c\u5206\u70ba\u88ab\u89c0\u5bdf\u8005\u548c\u89c0\u5bdf\u8005\u3002<\/p>\n\n\n\n<p>\u7576\u88ab\u89c0\u5bdf\u8005\u7684\u72c0\u614b\u767c\u751f\u8b8a\u5316\uff0c\u89c0\u5bdf\u8005\u5c31\u6703\u7372\u5f97\u4e8b\u4ef6\u901a\u77e5\u4e26\u4f5c\u51fa\u8655\u7406\u3002<\/p>\n\n\n\n<p>\u8a73\u7d30\u4ecb\u7d39\u53ef\u4ee5\u53c3\u8003<a rel=\"noreferrer noopener\" href=\"https:\/\/zh.wikipedia.org\/wiki\/%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F\" data-type=\"URL\" data-id=\"https:\/\/zh.wikipedia.org\/wiki\/%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F\" target=\"_blank\">\u7dad\u57fa\u767e\u79d1<\/a>\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">EventBus\u4ecb\u7d39<\/h2>\n\n\n\n<p>EventBus\u662f\u4e00\u500b\u91dd\u5c0dAndroid\u6700\u4f73\u5316\u7684\u4e8b\u4ef6\u767c\u9001&amp;\u8a02\u95b1\u6846\u67b6\uff0c\u76ee\u7684\u662f\u70ba\u4e86\u7c21\u5316\u5404\u500b\u5143\u4ef6\u9593\u7684\u901a\u8a0a\u3002<\/p>\n\n\n\n<p>EventBus\u4e3b\u8981\u662f\u7531\u4e8b\u4ef6\u3001\u4e8b\u4ef6\u8a02\u95b1\u8005\u3001\u4e8b\u4ef6\u767c\u5e03\u8005\u69cb\u6210\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e8b\u4ef6\u662f\u6307\u4efb\u4f55\u8981\u88ab\u767c\u9001\u7684\u8cc7\u6599\uff0c\u53ef\u4ee5\u662f\u4efb\u610f\u8cc7\u6599\u578b\u5225<\/li><li>\u4e8b\u4ef6\u8a02\u95b1\u8005\u662f\u6307\u63a5\u6536\u8cc7\u6599\u7684\u76ee\u6a19\u5143\u4ef6<\/li><li>\u4e8b\u4ef6\u767c\u5e03\u8005\u662f\u6307\u8ca0\u8cac\u767c\u9001\u8cc7\u6599\u7684\u5143\u4ef6<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>EventBus\u6709\u56db\u7a2e\u57f7\u884c\u7dd2\u6a21\u5f0f\uff0c\u5177\u9ad4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>MAIN\uff1a\u4e8b\u4ef6\u8655\u7406\u6703\u5728\u4e3b\u57f7\u884c\u7dd2\u4e2d\u57f7\u884c\uff0c\u5982\u679c\u8655\u7406\u7684\u6642\u9593\u904e\u9577\uff0c\u6703\u5f15\u8d77ANR\u3002<\/li><li>POSTING\uff1a\u9810\u8a2d\u7684\u8655\u7406\u6a21\u5f0f\uff0c\u8868\u793a\u767c\u9001\u8ddf\u63a5\u6536\u5728\u540c\u4e00\u500b\u57f7\u884c\u7dd2\u4e2d\u57f7\u884c\u3002<\/li><li>BACKGROUND\uff1a\u5728\u9019\u500b\u6a21\u5f0f\u4e0b\uff0c\u5982\u679c\u4e8b\u4ef6\u662f\u5728\u4e3b\u57f7\u884c\u7dd2\u767c\u9001\uff0c\u90a3\u9ebc\u63a5\u6536\u8005\u5c31\u6703\u5efa\u7acb\u4e00\u500b\u57f7\u884c\u7dd2\u8655\u7406\u4e8b\u4ef6\u3002\u5982\u679c\u4e8b\u4ef6\u662f\u5728\u5b50\u57f7\u884c\u7dd2\u767c\u9001\uff0c\u90a3\u63a5\u6536\u8005\u5c31\u6703\u5728\u767c\u5e03\u8005\u6240\u5728\u7684\u57f7\u884c\u7dd2\u4e2d\u8655\u7406\u4e8b\u4ef6\u3002\u6ce8\u610f\uff1a\u9019\u500b\u6a21\u5f0f\u4e0b\u7121\u6cd5\u9032\u884cUI\u76f8\u95dc\u7684\u64cd\u4f5c\uff01<\/li><li>ASYNC\uff1a\u4e0d\u7ba1\u4e8b\u4ef6\u5f9e\u54ea\u500b\u57f7\u884c\u7dd2\u767c\u5e03\uff0c\u63a5\u6536\u7aef\u90fd\u6703\u5728\u5b50\u57f7\u884c\u7dd2\u8655\u7406\u3002\u6ce8\u610f\uff1a\u9019\u500b\u6a21\u5f0f\u4e0b\u7121\u6cd5\u9032\u884cUI\u76f8\u95dc\u7684\u64cd\u4f5c\uff01<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7528\u6cd5<\/h2>\n\n\n\n<p>\u9019\u88e1\u4f7f\u7528\u4e00\u500bService\u4f5c\u70ba\u767c\u9001\u8005\u3001\u4e00\u500bActivity\u4f5c\u70ba\u63a5\u6536\u8005\u3002<\/p>\n\n\n\n<p>Service\u6bcf\u4e00\u79d2\u767c\u9001\u4e00\u500b\u6578\u503c+1\u7684\u4e8b\u4ef6\u3002\u7576Activity\u63a5\u6536\u5230\u4e8b\u4ef6\u5f8c\uff0c\u900f\u904eTextView\u986f\u793a\u4e8b\u4ef6\u7684\u6578\u503c\uff0c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1.\u9996\u5148\u5728build.gradle\u52a0\u5165EventBus\u5957\u4ef6\u5eab\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\" line-numbers\">dependencies {\n     implementation 'org.greenrobot:eventbus:3.0.0'\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>2.\u5ba3\u544a\u4e00\u500b\u81ea\u8a02\u985e\u5225\uff0c\u7576\u4f5cEventBus\u50b3\u905e\u7684\u53c3\u6578\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">data class MyEvent(\n    var counter: Int? = null\n)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>3.\u5efa\u7acbService\uff0c\u555f\u52d5\u65b9\u5f0f\u9019\u88e1\u7528Bind Service\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">class MyService : Service() {\n\n    private val mLocalBinder = LocalBinder()\n\n    private var counter = 0\n\n    private val handler = Handler()\n    private val timerCounter = object : Runnable {\n        override fun run() {\n            counter++\n\n            \/\/ \u767c\u9001\u4e8b\u4ef6\n            EventBus.getDefault().post(\n                MyEvent(counter)\n            )\n\n            handler.postDelayed(this, 1000)\n        }\n    }\n\n    override fun onBind(arg0: Intent?): IBinder {\n        return mLocalBinder\n    }\n\n    override fun onCreate() {\n        super.onCreate()\n        handler.post(timerCounter)\n    }\n\n    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {\n        return super.onStartCommand(intent, flags, startId)\n    }\n\n    override fun onUnbind(intent: Intent?): Boolean {\n        return super.onUnbind(intent)\n    }\n\n    override fun onDestroy() {\n        super.onDestroy()\n        handler.removeCallbacks(timerCounter)\n    }\n\n    inner class LocalBinder : Binder() {\n        val service: MyService = this@MyService\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>4.\u5728Activity\u5be6\u4f5c\u8655\u7406\u4e8b\u4ef6\u7684function\uff0c\u4e26\u52a0\u5165\u555f\u52d5Service\u7684\u7a0b\u5f0f\u78bc\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    private var mService: MyService? = null\n    private val mServiceConnection: ServiceConnection = object : ServiceConnection {\n        override fun onServiceConnected(name: ComponentName, serviceBinder: IBinder) {\n            mService = (serviceBinder as LocalBinder).service\n        }\n\n        override fun onServiceDisconnected(name: ComponentName) {}\n    }\n\n    private val eventBus: EventBus by lazy {\n        EventBus.getDefault()\n    }\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        mService = null\n        val it = Intent(this, MyService::class.java)\n        bindService(it, mServiceConnection, BIND_AUTO_CREATE) \/\/\u7d81\u5b9aService\n    }\n\n    override fun onResume() {\n        super.onResume()\n\n        eventBus.register(this)\n    }\n\n    override fun onPause() {\n        super.onPause()\n\n        eventBus.unregister(this)\n    }\n\n    override fun onDestroy() {\n        super.onDestroy()\n\n        mService = null\n        unbindService(mServiceConnection)\n    }\n\n    \/\/ \u8a02\u95b1\u4e8b\u4ef6\n    @Subscribe(threadMode = ThreadMode.MAIN)\n    fun onEvent(event: MyEvent) {\n        textView.text = \"count: ${event.counter}\"\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6700\u5f8c\u57f7\u884c\u61c9\u7528\u7a0b\u5f0f\uff0c\u5373\u53ef\u770b\u5230TextView\u4e0a\u7684\u6578\u503c\u6bcf\u79d2+1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89c0\u5bdf\u8005\u6a21\u5f0f\u662f\u8edf\u9ad4\u8a2d\u8a08\u6a21\u5f0f\u7684\u4e00\u7a2e\uff0c\u5206\u70ba\u88ab\u89c0\u5bdf\u8005\u548c\u89c0\u5bdf\u8005\u3002 \u7576\u88ab\u89c0\u5bdf\u8005\u7684\u72c0\u614b\u767c\u751f\u8b8a\u5316\uff0c\u89c0\u5bdf\u8005\u5c31\u6703\u7372\u5f97\u4e8b\u4ef6\u901a\u77e5\u4e26\u4f5c\u51fa &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,19],"class_list":["post-1014","post","type-post","status-publish","format-standard","hentry","category-2","tag-android-app","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1014"}],"version-history":[{"count":4,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1014\/revisions"}],"predecessor-version":[{"id":1018,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/1014\/revisions\/1018"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}