Android EventBus的基本用法(kotlin版)

别等时光非礼了梦想. 提交于 2020-12-16 12:10:15

EventBus

https://github.com/greenrobot/EventBus

EventBus是一种用于Android的事件发布-订阅总线。它简化了应用程序内各个组件之间进行通信的复杂度,尤其是碎片之间进行通信的问题,可以避免由于使用广播通信而带来的诸多不便。

接收方


class MainActivity : AppCompatActivity() {
   
   

    override fun onCreate(savedInstanceState: Bundle?) {
   
   
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        EventBus.getDefault().register(this)//注册,重复注册会导致崩溃
        MainActivity2.start(this)
    }

    override fun onDestroy() {
   
   
        super.onDestroy()
        EventBus.getDefault().unregister(this)//解绑
    }

    //接收消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: MessageEvent) {
   
   
        when (event.type) {
   
   
            MessageType.ShowLog -> {
   
   
                Log.e(TAG, "onMessageEvent: " + event.getString())
            }
            MessageType.ShowToast -> {
   
   
                Toast.makeText(this, "onMessageEvent: " + event.getString(), Toast.LENGTH_SHORT).show()
            }
        }
    }

}

发送方

    fun onClick(view: View) {
   
   
        when (view.id) {
   
   
            R.id.bt1 -> {
   
   
                EventBus.getDefault().post(MessageEvent(MessageType.ShowToast).put("hello"))
            }
            R.id.bt2 -> {
   
   
                EventBus.getDefault().post(MessageEvent(MessageType.ShowLog).put("你好"))
            }
        }
    }

发送的类型(封装后只需要一个类即可,MessageType 来区分)

package maka.components.myeventbustest

import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable

private const val KEY_INT = "key_int"
private const val KEY_STRING = "key_string"
private const val KEY_BOOL = "key_bool"
private const val KEY_SERIALIZABLE = "key_serializable"
private const val KEY_PARCELABLE = "key_parcelable"


data class MessageEvent(var type: MessageType) {
   
   

    var bundle = Bundle()

    fun put(value: Int): MessageEvent {
   
   
        bundle.putInt(KEY_INT, value)
        return this
    }

    fun put(value: String): MessageEvent {
   
   
        bundle.putString(KEY_STRING, value)
        return this
    }

    fun put(value: Boolean): MessageEvent {
   
   
        bundle.putBoolean(KEY_BOOL, value)
        return this
    }

    fun put(value: Serializable): MessageEvent {
   
   
        bundle.putSerializable(KEY_SERIALIZABLE, value)
        return this
    }

    fun put(value: Parcelable): MessageEvent {
   
   
        bundle.putParcelable(KEY_PARCELABLE, value)
        return this
    }

    fun put(key: String, value: Int): MessageEvent {
   
   
        bundle.putInt(key, value)
        return this
    }

    fun put(key: String, value: String): MessageEvent {
   
   
        bundle.putString(key, value)
        return this
    }

    fun put(key: String, value: Boolean): MessageEvent {
   
   
        bundle.putBoolean(key, value)
        return this
    }

    fun put(key: String, value: Serializable): MessageEvent {
   
   
        bundle.putSerializable(key, value)
        return this
    }

    fun put(key: String, value: Parcelable): MessageEvent {
   
   
        bundle.putParcelable(key, value)
        return this
    }

    //===============================================================

    fun getInt(): Int {
   
   
        return bundle.getInt(KEY_INT)
    }

    fun getString(): String? {
   
   
        return bundle.getString(KEY_STRING)
    }

    fun getBoolean(): Boolean {
   
   
        return bundle.getBoolean(KEY_BOOL)
    }

    fun <T : Serializable> getSerializable(): Serializable {
   
   
        return bundle.getSerializable(KEY_SERIALIZABLE) as T
    }

    fun <T : Parcelable> getParcelable(): T? {
   
   
        return bundle.getParcelable<T>(KEY_PARCELABLE)
    }

    fun getInt(key: String): Int {
   
   
        return bundle.getInt(key)
    }

    fun getString(key: String): String? {
   
   
        return bundle.getString(key)
    }

    fun getBoolean(key: String): Boolean {
   
   
        return bundle.getBoolean(key)
    }

    fun <T : Serializable> getSerializable(key: String): Serializable {
   
   
        return bundle.getSerializable(key) as T
    }

    fun <T : Parcelable> getParcelable(key: String): T? {
   
   
        return bundle.getParcelable<T>(key)
    }

}

enum class MessageType {
   
   
    ShowToast,
    ShowLog,
}


关于粘性事件的发送(先发送后注册)

  • 发送方,postSticky
  • 接收方,sticky = true
  • 先发送后注册

接收(粘性事件)

    override fun onStart() {
   
   
        super.onStart()
        EventBus.getDefault().register(this)//注册,重复注册会导致崩溃
    }

    override fun onStop() {
   
   
        super.onStop()
        EventBus.getDefault().unregister(this)//解绑
    }

    //接收消息
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onMessageEvent(event: MessageEvent) {
   
   
        when (event.type) {
   
   
            MessageType.ShowLog -> {
   
   
                viewBinding.tvShow.text = event.getString()
                Log.e(TAG, "onMessageEvent: " + event.getString())
            }
            MessageType.ShowToast -> {
   
   
                Toast.makeText(this, "onMessageEvent: " + event.getString(), Toast.LENGTH_SHORT)
                    .show()
            }
        }
    }

发送(粘性事件)

    fun onClick(view: View) {
   
   
        when (view.id) {
   
   
            R.id.bt1 -> {
   
   
                EventBus.getDefault().postSticky(MessageEvent(MessageType.ShowToast).put("hello"))
            }
            R.id.bt2 -> {
   
   
                EventBus.getDefault().postSticky(MessageEvent(MessageType.ShowLog).put("你好"))
            }
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!