1 Eeventbus 在處理消息通信的時候是比通常的出發辦法好用的得,最新使用了eventbus 的粘性事件 首先你需要傳遞一個消息bean,
EventBus.getDefault().postSticky(messageStatusResultBean);
2 調用上面的方法 就會把這個objoct 放到里面
private final Map<Class<?>, Object> stickyEvents;
3 在要處理的地方獲取消息bean
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN) public void getPushMsg(MessageStatusResultBean messageStatusResultBean) { }
4 粘性事件要收到移除
EventBus.getDefault().removeStickyEvent(MessageStatusResultBean.class);
5 他是如何調用的呢,首先我們會在oncreate的時候做這樣的一件事情
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
6 然后我們來看看 register 這個方法 調用了subcribe方法
public void register(Object subscriber) { Class<?> subscriberClass = subscriber.getClass(); List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass); synchronized (this) { for (SubscriberMethod subscriberMethod : subscriberMethods) { subscribe(subscriber, subscriberMethod); //調用了subcribe方法
} } }
7 接做看 subscribe 方法
// Must be called in synchronized block private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) { //.... if (subscriberMethod.sticky) { if (eventInheritance) { // Existing sticky events of all subclasses of eventType have to be considered. // Note: Iterating over all events may be inefficient with lots of sticky events, // thus data structure should be changed to allow a more efficient lookup // (e.g. an additional map storing sub classes of super classes: Class -> List<Class>). Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet(); for (Map.Entry<Class<?>, Object> entry : entries) { Class<?> candidateEventType = entry.getKey(); if (eventType.isAssignableFrom(candidateEventType)) { Object stickyEvent = entry.getValue(); checkPostStickyEventToSubscription(newSubscription, stickyEvent); } } } else { Object stickyEvent = stickyEvents.get(eventType); checkPostStickyEventToSubscription(newSubscription, stickyEvent); } } }
8 接下來走到
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) { switch (subscription.subscriberMethod.threadMode) { case POSTING: invokeSubscriber(subscription, event); break; case MAIN: if (isMainThread) { invokeSubscriber(subscription, event); } else { mainThreadPoster.enqueue(subscription, event); } break; case BACKGROUND: if (isMainThread) { backgroundPoster.enqueue(subscription, event); } else { invokeSubscriber(subscription, event); } break; case ASYNC: asyncPoster.enqueue(subscription, event); break; default: throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode); } }
9 接下來就是去處理了,會根據線程模式的不同處理不同的事件 ,在主線程是通過handle來處理的
@Override public void handleMessage(Message msg) { boolean rescheduled = false; try { long started = SystemClock.uptimeMillis(); while (true) { PendingPost pendingPost = queue.poll(); if (pendingPost == null) { synchronized (this) { // Check again, this time in synchronized pendingPost = queue.poll(); if (pendingPost == null) { handlerActive = false; return; } } } eventBus.invokeSubscriber(pendingPost); long timeInMethod = SystemClock.uptimeMillis() - started; if (timeInMethod >= maxMillisInsideHandleMessage) { if (!sendMessage(obtainMessage())) { throw new EventBusException("Could not send handler message"); } rescheduled = true; return; } } } finally { handlerActive = rescheduled; } }