android 自定义event,Android 自定义EventBus学习

import android.os.Handler

import android.os.Looper

import java.lang.RuntimeException

import java.util.concurrent.Executors

object EventBus {

private val eventMap by lazy { HashMap>() }

private val handler by lazy { Handler(Looper.getMainLooper()) }

private val executorService by lazy { Executors.newCachedThreadPool() }

fun register(context: Any) {

val list = eventMap[context] ?: findSubscribeMethod(context)

eventMap[context] = list

}

fun unRegister(context: Any) {

eventMap.remove(context)

}

private fun findSubscribeMethod(context: Any): List {

val list = ArrayList()

var clazz: Class? = context.javaClass

while (clazz != null) {

if (clazz.name.startsWith("java.") || clazz.name.startsWith("javax.") || clazz.name.startsWith("android.")) break

val methods = clazz.methods

for (method in methods) {

val subscribe = method.getAnnotation(Subscribe::class.java) ?: continue

val types = method.parameterTypes

if (types.size != 1) {

throw RuntimeException("EventBus only allow one parameter")

}

list.add(SubscribeMethod(method, subscribe.threadMode, types[0]))

}

clazz = clazz.superclass

}

return list

}

fun post(type: Any) {

val iterator = eventMap.keys.iterator()

while (iterator.hasNext()) {

val any = iterator.next()

val list = eventMap[any] ?: continue

for (method in list) {

if (method.type.isAssignableFrom(type.javaClass)) {

when (method.threadMode) {

ThreadMode.MAIN -> {

if (Looper.myLooper() == Looper.getMainLooper()) {

invoke(method, any, type)

} else {

handler.post { invoke(method, any, type) }

}

}

ThreadMode.BG -> {

if (Looper.myLooper() == Looper.getMainLooper()) {

executorService.execute { invoke(method, any, type) }

} else {

invoke(method, any, type)

}

}

}

}

}

}

}

private fun invoke(method: SubscribeMethod, any: Any, type: Any) {

method.method.invoke(any, type)

}

}

@Target(AnnotationTarget.FUNCTION)

@Retention(AnnotationRetention.RUNTIME)

annotation class Subscribe(val threadMode: ThreadMode = ThreadMode.MAIN)

class SubscribeMethod(var method: Method, var threadMode: ThreadMode, var type: Class)

enum class ThreadMode {

MAIN, BG

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值