标签:
代码里面发送粘性事件代码如下:
// 发送Sticky事件 EventBus.getDefault().postSticky(new User("soyoungboy", "西安财经学院"), "soyoungboy");
private List<EventType> mStickyEvents = Collections .synchronizedList(new LinkedList<EventType>());
/** * 发布Sticky事件,tag为EventType.DEFAULT_TAG * * @param event */ public void postSticky(Object event) { postSticky(event, EventType.DEFAULT_TAG); } /** * 发布含有tag的Sticky事件 * * @param event 事件 * @param tag 事件tag */ public void postSticky(Object event, String tag) { EventType eventType = new EventType(event.getClass(), tag); eventType.event = event; mStickyEvents.add(eventType); // 处理sticky事件 // mDispatcher.handleStickyEvent(eventType, null); }
EventBus.getDefault().registerSticky(this);
/** * 以sticky的形式注册,则会在注册成功之后迭代所有的sticky事件 * * @param subscriber */ public void registerSticky(Object subscriber) { this.register(subscriber); // 处理sticky事件 mDispatcher.dispatchStickyEvents(subscriber); }
register代码不进行分析,因为前面讲过,就是将@ subscriber的订阅者对象放入map集合里面去
void dispatchStickyEvents(Object subscriber) { for (EventType eventType : mStickyEvents) { handleStickyEvent(eventType, subscriber); } }
/** * 处理单个Sticky事件 * * @param eventType * @param aEvent */ private void handleStickyEvent(EventType eventType, Object subscriber) { //从缓存获取eventTypes内容 List<EventType> eventTypes = getMatchedEventTypes(eventType, eventType.event); // 事件 Object event = eventType.event; //循环遍历eventType集合 for (EventType foundEventType : eventTypes) { Log.e("", "### 找到的类型 : " + foundEventType.paramClass.getSimpleName() + ", event class : " + event.getClass().getSimpleName()); //从订阅者的map集合里面获取指定的订阅者集合的list集合,根据eventType final List<Subscription> subscriptions = mSubcriberMap.get(foundEventType); if (subscriptions == null) { continue; } //遍历list集合 for (Subscription subItem : subscriptions) { final ThreadMode mode = subItem.threadMode; EventHandler eventHandler = getEventHandler(mode); // 如果订阅者为空,那么该sticky事件分发给所有订阅者.否则只分发给该订阅者 if (isTarget(subItem, subscriber) && (subItem.eventType.equals(foundEventType) || subItem.eventType.paramClass .isAssignableFrom(foundEventType.paramClass))) { // 处理事件 eventHandler.handleEvent(subItem, event); } } } }
/** * @param subscription * @param event */ public void handleEvent(final Subscription subscription, final Object event) { mUIHandler.post(new Runnable() { @Override public void run() { mEventHandler.handleEvent(subscription, event); } }); }
/** * 将订阅的函数执行在异步线程中 * * @param subscription * @param event */ public void handleEvent(final Subscription subscription, final Object event) { mDispatcherThread.post(new Runnable() { @Override public void run() { mEventHandler.handleEvent(subscription, event); } }); }
/** * handle the event * * @param subscription * @param event */ public void handleEvent(Subscription subscription, Object event) { if (subscription == null || subscription.subscriber.get() == null) { return; } try { // 执行 subscription.targetMethod.invoke(subscription.subscriber.get(), event); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
【第五篇】androidEventbus源代码阅读和分析之发送粘性事件和接收粘性事件代码分析
标签:
原文地址:http://www.cnblogs.com/androidsuperman/p/4679982.html