1.EventBus使用
EventBus.getDefault().register(this); //注冊
EventBus.getDefault().unregister(this); //移除
EventBus.getDefault().post(new Object());//發(fā)送事件
/*
注冊(Register):訂閱者通過調用EventBus的register方法注冊到EventBus中,并指定自己感興趣的事件類型。注冊時,EventBus會利用反射機制查找訂閱者類中帶有@Subscribe注解的方法,并將這些方法與事件類型進行關聯。
發(fā)布(Post):發(fā)布者通過調用EventBus的post方法發(fā)布一個事件。EventBus會遍歷所有已注冊的訂閱者,找到與事件類型匹配的訂閱者,并調用其對應的處理方法。
處理(Handle):訂閱者接收到事件后,會執(zhí)行與事件類型匹配的處理方法。處理方法的執(zhí)行線程可以根據@Subscribe注解中的threadMode屬性進行指定,包括POSTING(發(fā)布線程)、MAIN(主線程)、BACKGROUND(后臺線程)和ASYNC(異步線程)等。
*/
2.EventBus.getDefault().register(this)流程分析
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
/通過反射拿到類中所有含有@Subscribe注解的方法集合
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
List<SubscriberMethod> list=new ArrayList<SubscriberMethod>();
Method[] methods = subscriberClass.getDeclaredMethods(); //通過反射拿到類中所有方法集合
for (Method method : methods) {
//找到還有Subscribe方法的集合
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
Class<?>[] parameterTypes = method.getParameterTypes();
if (subscribeAnnotation != null) {
Class<?> eventType = parameterTypes[0];
/*構造訂閱方法SubscriberMethod類*/
list.add(new SubscriberMethod(method,eventType,threadMode));
}
}
}
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType= new HashMap<>();
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
//構建一個Subscription訂閱類,把方法和觀察者進行關聯
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//通過訂閱方法類,得到事件類型
Class<?> eventType = subscriberMethod.eventType;
//實例化訂閱者集合
CopyOnWriteArrayList<Subscription> subscriptions=subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
subscriptions=new CopyOnWriteArrayList<>();
}
//訂閱者集合中添加一個新的訂閱
subscriptions.add(newSubscription);
//map集合保存key=eventType ,value=subscriptions集合列表
subscriptionsByEventType.put(eventType, subscriptions);
}
3.Event.getDefault().post(new PostBean())流程解析
public void post(Object event) {
for (Subscription subscription : subscriptions) {
//根據訂閱的threadMode類型,決定實在主線程還是在異步線程或者后臺線程中執(zhí)行invoke方法
switch (subscription.subscriberMethod.threadMode) {
case POSTING:
subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
break;
case MAIN:
subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
break;
case BACKGROUND: //后臺線程
backgroundPoster.enqueue(subscription, event); //啟動線程池執(zhí)行
break;
case ASYNC://異步線程
asyncPoster.enqueue(subscription, event);//啟動線程池執(zhí)行
break;
}
}
}
public void enqueue(Subscription subscription, Object event) {
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
queue.enqueue(pendingPost);
eventBus.getExecutorService().execute(this);//啟動線程池執(zhí)行
}