疯狂地重复造轮子系列——CEventBus,事件发布总线框架,模仿EventBus实现的简易版,使用方法与EventBus大同小异。
|
2 months ago | |
---|---|---|
.idea | 2 months ago | |
app | 2 months ago | |
ceventbus | 2 months ago | |
gradle | 2 months ago | |
.gitignore | 2 months ago | |
README.md | 2 months ago | |
build.gradle | 2 months ago | |
gradle.properties | 2 months ago | |
gradlew | 2 months ago | |
gradlew.bat | 2 months ago | |
settings.gradle | 2 months ago |
疯狂地重复造轮子系列——CEventBus,事件发布总线框架, 模仿EventBus实现的简易版,使用方法与EventBus大同小异。
在Activity的onCreate方法中进行注册:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CEventBus.getDefault().register(this);
}
在Activity的onDestroy方法中进行反注册:
@Override
protected void onDestroy() {
super.onDestroy();
CEventBus.getDefault().unRegister(this);
}
在需要接收事件的方法上添加@Subscribe,并且接收事件的方法只能有一个形参,例如:
@Subscribe
public void getEvent(Event event) { // 正确,可以接收到事件
Log.d("CYTAG", "2,当前线程 --->>> " + Thread.currentThread().getName()
+ "\t消息 --->>>" + event.getMsg());
}
@Subscribe
public void getEvent(Event event, String msg) { // 错误,不会接收到事件
Log.d("CYTAG", "2,当前线程 --->>> " + Thread.currentThread().getName()
+ "\t消息 --->>>" + event.getMsg());
}
发送事件
CEventBus.getDefault().post(new Event("来自SecondActivity的消息"));
注解说明,@Subscribe注解可以传入三个值:
基于反射实现:
注册时通过反射获取接收事件方法添加到集合中
发送事件时通过获取形参为相同类型的的接收事件方法通过invoke反射执行
反注册不需要反射