1 注册事件
if (!MyEventBus.getDefault().isRegistered(this)) {
MyEventBus.getDefault().register(this);
}
2 反注册事件
if (MyEventBus.getDefault().isRegistered(this)) {
MyEventBus.getDefault().unRegister(this);
}
3 判断当前是否注册了事件
MyEventBus.getDefault().isRegistered(this)
4 事件回掉
/**
* 运行在UI线程
* @param msg
*/
public void onEventUIThread(MyEvent msg) {
Log.e("TAG","onEventUIThread----->" + msg.eventName + "........" + msg.eventData);
}
/**
* 运行在子线程
* @param msg
*/
public void onEventNewThread(MyEvent msg) {
Log.e("TAG","onNewThread----->" + msg.eventName + "........" + msg.eventData);
}
/**
* 运行在当前线程
* @param msg
*/
public void onEventCurrentThread(MyEvent msg) {
Log.e("TAG","onEventCurrentThread----->" + msg.eventName + "........" + msg.eventData);
}
5 发送普通事件
MyEventBus.getDefault().postSticky(new MyEvent("测试数据",false,System.currentTimeMillis()));
6 发送粘性事件
MyEventBus.getDefault().postSticky(new MyEvent("测试数据",true,System.currentTimeMillis()));
7 整体EventBus类
/***
* 日期: 2019/4/3 11:52
* 姓名: bjs
* 说明: 自定义EventBus类
*/
public class MyEventBus {
private MyEvent myStickyEvent;
//运行在UI线程
private static final String MATHER_NAME_UI_THREAD = "onEventUIThread";
//运行在子线程
private static final String MATHER_NAME_NEW_THREAD = "onEventNewThread";
//运行在当前线程
private static final String MATHER_NAME_CURRENT_THREAD = "onEventCurrentThread";
private static ArrayList arrayList = new ArrayList<>();
private volatile static MyEventBus instance;
private Handler handlerMain = new Handler(Looper.getMainLooper());
private MyEventBus() {
}
public static MyEventBus getDefault() {
if (instance == null) {
synchronized (MyEventBus.class) {
if (instance == null) {
instance = new MyEventBus();
}
}
}
return instance;
}
/**
* 注册事件
* @param subscriber
*/
public void register(Activity subscriber) {
if (!arrayList.contains(subscriber)) {
arrayList.add(subscriber);
}
//判断是否有粘性事件
if (myStickyEvent != null) {
openMethod(subscriber,myStickyEvent);
}
}
/**
* 反注册事件
* @param subscriber
*/
public void unRegister(Activity subscriber) {
for (int i = 0; i < arrayList.size(); i++) {
if (arrayList.contains(subscriber)) {
arrayList.remove(subscriber);
}
}
}
/**
* 判断当前是否注册了事件
* @param subscriber
* @return
*/
public boolean isRegistered(Activity subscriber) {
for (int i = 0; i < arrayList.size(); i++) {
return arrayList.contains(subscriber);
}
return false;
}
/**
* 发送普通的事件
* @param eventMsg
*/
public void post(MyEvent eventMsg) {
//通过反射获取方法,
for (int i = 0; i < arrayList.size(); i++) {
openMethod(arrayList.get(i),eventMsg);
}
}
/**
* 发送粘性事件
* @param eventMsg
*/
public void postSticky(MyEvent eventMsg) {
if (eventMsg.isSticky()) {
myStickyEvent = eventMsg;
}
post(eventMsg);
}
/**
* 清除内存中的粘性事件
*/
public void clearSticky() {
myStickyEvent = null;
}
private void openMethod(Activity activity, MyEvent eventMsg) {
try {
Class cls = activity.getClass();
//第一个参数是被调用方法的名称,后面接着这个方法的形参类型
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
switch (method.getName()) {
case MATHER_NAME_UI_THREAD://UI线程
handlerMain.post(()->invokeMethod(activity,method,eventMsg));
break;
case MATHER_NAME_NEW_THREAD://子线程
new Thread(()->invokeMethod(activity,method,eventMsg)).start();
break;
case MATHER_NAME_CURRENT_THREAD://当前线程
invokeMethod(activity,method,eventMsg);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void invokeMethod(Activity activity, Method method, MyEvent eventMsg) {
try {
method.invoke(activity,eventMsg);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
8 整体事件类
/***
* 日期: 2019/4/3 11:56
* 姓名: bjs
* 说明: 自定义消息类
*/
public class MyEvent {
public String eventName; //事件名称
public boolean sticky; //是否是粘性事件
public Object eventData; //事件携带的数据
public MyEvent(String eventName, boolean sticky, Object eventData) {
this.eventName = eventName;
this.sticky = sticky;
this.eventData = eventData;
}
}
9 整体使用类
/***
* 日期: 2019/4/3 10:46
* 姓名: bjs
* 说明: 使用EventBus的类
*/
public class BeforeActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_before);
initView();
//注册
if (!MyEventBus.getDefault().isRegistered(this)) {
MyEventBus.getDefault().register(this);
}
}
private void initView() {
findViewById(R.id.btn1).setOnClickListener(v->{
//发送普通事件
MyEventBus.getDefault().post(new MyEvent("测试数据",false,System.currentTimeMillis()));
});
findViewById(R.id.btn2).setOnClickListener(v->{
//发送粘性事件
MyEventBus.getDefault().postSticky(new MyEvent("测试数据",true,System.currentTimeMillis()));
});
}
/**
* 运行在UI线程
* @param msg
*/
public void onEventUIThread(MyEvent msg) {
Log.e("TAG","onEventUIThread----->" + msg.eventName + "........" + msg.eventData);
}
/**
* 运行在子线程
* @param msg
*/
public void onEventNewThread(MyEvent msg) {
Log.e("TAG","onNewThread----->" + msg.eventName + "........" + msg.eventData);
}
/**
* 运行在当前线程
* @param msg
*/
public void onEventCurrentThread(MyEvent msg) {
Log.e("TAG","onEventCurrentThread----->" + msg.eventName + "........" + msg.eventData);
}
@Override
protected void onDestroy() {
super.onDestroy();
//反注册
if (MyEventBus.getDefault().isRegistered(this)) {
MyEventBus.getDefault().unRegister(this);
}
}
}
-------------------------------分------------割----------线----------------------------------------------------------
自己为了加深理解编写,若有误望指正,不喜勿喷~~