編輯:關於Android編程
最近因為換工作的一些瑣事搞的我一個頭兩個大,也沒怎麼去學新東西,實在是有些愧疚。新項目用到了EventBus3.0,原來只是聽說EventBus的鼎鼎大名,一直沒仔細研究過。趁著周末有些時間,研究下代碼,也算沒有虛度光陰。
EventBus GitHub : https://github.com/greenrobot/EventBus
EventBus
是greenrobot出品的一個用於Android中事件發布/訂閱的庫。以前傳遞對象可能通過接口、廣播、文件等等,尤其像同一個Activity
兩個Fragment
之間采用接口傳遞對象,十分的麻煩,而且耦合度較高。使用EventBus
之後,這些將不再是問題。盜用GiHub上EventBus的一張圖。
vcq9vPvPws7EoaM8L3A+DQo8aDEgaWQ9"eventbus30的使用">EventBus3.0的使用
新建兩個Activity
,花3s掃一下即可。代碼如下:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注冊EventBus
EventBus.getDefault().register(this);
startActivity(new Intent(this,SecondActivity.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
// 反注冊EventBus
EventBus.getDefault().unregister(this);
}
// 主線程調用
@Subscribe(threadMode = ThreadMode.MAIN)
public void eventBusMain(String str){
Log.i("TAG", "MAIN:"+str+" Thread="+Thread.currentThread().getId());
}
// 1.發布線程為主線程,新開線程調用
// 2.發布線程為子線程,發布線程調用
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void eventBusBg(String str){
Log.i("TAG", "BACKGROUND:"+str+" Thread="+Thread.currentThread().getId());
}
// 在發布線程調用,默認值
@Subscribe(threadMode = ThreadMode.POSTING)
public void eventBusPosting(String str){
Log.i("TAG", "POSTING:"+str+" Thread="+Thread.currentThread().getId());
}
// 每次都新開線程調用
@Subscribe(threadMode = ThreadMode.ASYNC)
public void eventBusAsync(String str){
Log.i("TAG", "ASYNC:"+str+" Thread="+Thread.currentThread().getId());
}
}
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
EventBus.getDefault().post("from second activity mainThread: info");
Log.i("TAG", "Post thread="+Thread.currentThread().getId());
new Thread(new Runnable() {
@Override
public void run() {
EventBus.getDefault().post("from second activity childThread: info");
Log.i("TAG", "Post thread="+Thread.currentThread().getId());
}
}).start();
}
}
在MainActivity
的onCreate()
/onDestroy()
中分別注冊/反注冊EventBus
。然後寫了四個測試ThreadMode
的方法,調用時機注釋的很清楚,就不贅述了。最後在SecondActivity
的主線程和子線程中分別調用Post()
方法,注意,這裡Post()
方法的參數為Object
類型,這也就意味著我們傳遞任何對象都是可以的,例如JavaBean
、List
等等都是可以的,這裡為了方便演示直接傳遞了String
。Log信息如下:
第一張圖中發布者發送線程為主線程,即Post thread = 1
,在訂閱者收到消息時,ThreadMode = Main
和ThreadMode = Posting
的方法都在主線程調用,ASYNC
和BACKGROUND
都新開了線程。圖二對比注釋同理。
除此之外,Subscribe
注解還支弛喎?/kf/yidong/wp/" target="_blank" class="keylink">WPGNvZGU+cHJpb3JpdHk8L2NvZGU+us08Y29kZT5zdGlja3k8L2NvZGU+yvTQ1KGjPGNvZGU+cHJpb3JpdHk8L2NvZGU+yejWw73TytXV37XE08XPyLy2o6zErMjP1rXOqjCho9PFz8i8trjftcS3vbeoz8ixu7X308OjrNTat723qLX308PN6rPJuvO/ydLUtffTwzxjb2RlPkV2ZW50QnVzLmdldERlZmF1bHQoKS5jYW5jZWxFdmVudERlbGl2ZXJ5KGV2ZW50KSA7PC9jb2RlPtbV1rnTxc/IvLa1zbXEt723qLXEtffTw6GjPGNvZGU+c3RpY2t5PC9jb2RlPs6q1bPQ1MrCvP6jrMSsyM/OqrnYsdXXtMysoaPE3Lm7ytW1vbap1MTWrsewt6LLzbW9tcTX7rrz0rvM9c/7z6KjrLKix9K3osvNtcS3vbeosrvU2crHPGNvZGU+cG9zdCgpPC9jb2RlPrb4ysc8Y29kZT5wb3N0U3RpY2t5KCk8L2NvZGU+oaM8L3A+DQo8aDEgaWQ9"eventbus30源碼解析">EventBus3.0源碼解析
EventBus
是Very的好用。耦合度大大的降低,而且代碼十分優雅。它是怎麼就做到了這麼優雅的呢?知其然,知其所以然。下面就開始一步步的分析。
Subscribe
對注解不了解的同學可以看下這篇博客。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Subscribe {
ThreadMode threadMode() default ThreadMode.POSTING;
/**
* If true, delivers the most recent sticky event (posted with
* {@link EventBus#postSticky(Object)}) to this subscriber (if event available).
*/
boolean sticky() default false;
/** Subscriber priority to influence the order of event delivery.
* Within the same delivery thread ({@link ThreadMode}), higher priority subscribers will receive events before
* others with a lower priority. The default priority is 0. Note: the priority does *NOT* affect the order of
* delivery among subscribers with different {@link ThreadMode}s! */
int priority() default 0;
}
public enum ThreadMode {
POSTING,
MAIN,
BACKGROUND,
ASYNC
}
注解Subscribe
在運行時解析,且只能加在METHOD
上。其中有三個方法,threadMode()
返回類型ThreadMode
為枚舉類型,默認值為POSTING
,sticky()
默認返回false
,priority()
默認返回0。
Register
流程EventBus#getDefault()
public EventBus() {
this(DEFAULT_BUILDER);
}
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
EventBus
采用雙重校驗鎖設計為一個單例模式,奇怪的在於雖然設計為單例模式,但是構造方法確實public
類型,這不是坑爹嘛!難道greenrobot
在設計EventBus
獲取實例方法的時候在打LOL,一不小心打錯了?原來啊,EventBus
默認支持一條事件總線,通常是通過getDefault()
方法獲取EventBus
實例,但也能通過直接new EventBus
這種最簡單的方式獲取多條事件總線,彼此之間完全分開。設計之思想不禁讓人拍案叫絕。
EventBus#register()
public void register(Object subscriber) {
Class subscriberClass = subscriber.getClass();
List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
首先得到訂閱者的報名.類名,即哪個具體類注冊。然後調用subscriberMethodFinder.findSubscriberMethods(subscriberClass)
,從方法名和返回值來看,findSubscriberMethods()
的作用應該是遍歷查找訂閱者中所有的訂閱方法。
SubscriberMethodFinder#findSubscriberMethods()
List findSubscriberMethods(Class subscriberClass) {
// 查找緩存
List subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
// 緩存中有則直接返回
return subscriberMethods;
}
// 默認false
if (ignoreGeneratedIndex) {
subscriberMethods = findUsingReflection(subscriberClass);
} else {
subscriberMethods = findUsingInfo(subscriberClass);
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
// 加入緩存
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
注意subscriberMethods.isEmpty()
,如果注冊了EventBus
,但卻沒有使用注解Subscribe
是會出現EventBusException
異常的。下面跟進findUsingInfo()
方法。
SubscriberMethodFinder#findUsingInfo()
private List findUsingInfo(Class subscriberClass) {
// 從FIND_STATE_POOL數組中查找FindState,命中返回,否則直接new
FindState findState = prepareFindState();
// 初始化FindState
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
findState.subscriberInfo = getSubscriberInfo(findState);
// findState.subscriberInfo默認null
if (findState.subscriberInfo != null) {
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
findUsingReflectionInSingleClass(findState);
}
// 將findState.clazz變為改類的父類
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
// SubscriberMethodFinder$FindState#initForSubscriber()
void initForSubscriber(Class subscriberClass) {
this.subscriberClass = clazz = subscriberClass;
skipSuperClasses = false;
subscriberInfo = null;
}
findState.subscriberInfo
默認null
,那麼就進入到findUsingReflectionInSingleClass(findState)
,先看下這個方法,等下還要返回來看。
SubscriberMethodFinder#findUsingReflectionInSingleClass()
private static final int MODIFIERS_IGNORE = Modifier.ABSTRACT | Modifier.STATIC | BRIDGE | SYNTHETIC;
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
// 獲取到類中所有的方法
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
for (Method method : methods) {
// 獲取方法的修飾符
int modifiers = method.getModifiers();
// 必須被public修飾,而且不能為MODIFIERS_IGNORE
// MODIFIERS_IGNORE定義為ABSTRACT、STATIC、BRIDGE、SYNTHETIC。
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
// 獲取方法所有參數類型
Class[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1) {
// 是否有Subscribe注解標簽
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
// 帶有Subscribe注解標簽的方法的第一個參數類型
Class eventType = parameterTypes[0];
// 關聯method, eventType到anyMethodByEventType
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
// 構造SubscriberMethod,並且添加到findState.subscriberMethods
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
// strictMethodVerification 默認為false,不會拋出異常,但還是建議符合規范
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}
// `SubscriberMethodFinder#checkAdd()`
boolean checkAdd(Method method, Class eventType) {
// 2 level check: 1st level with event type only (fast), 2nd level with complete signature when required.
// Usually a subscriber doesn't have methods listening to the same event type.
Object existing = anyMethodByEventType.put(eventType, method);
if (existing == null) {
return true;
} else {
...
}
接下來返回SubscriberMethodFinder#findUsingInfo()
接著看,在findUsingInfo()
中循環執行完後return getMethodsAndRelease(findState)
static class FindState {
final List subscriberMethods = new ArrayList<>();
...
}
private List getMethodsAndRelease(FindState findState) {
List subscriberMethods = new ArrayList<>(findState.subscriberMethods);
// 置空findState
findState.recycle();
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
if (FIND_STATE_POOL[i] == null) {
FIND_STATE_POOL[i] = findState;
break;
}
}
}
return subscriberMethods;
}
在getMethodsAndRelease()
中將findState
置空,存放進FIND_STATE_POOL
數組,最後返回findState.subscriberMethods
。返回EventBus#register()
。
EventBus#register()
public void register(Object subscriber) {
Class subscriberClass = subscriber.getClass();
List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
調用SubscriberMethodFinder#findSubscriberMethods()
後,以List
形式返回了訂閱者所有的訂閱事件。然後遍歷執行subscribe()
方法。看樣子應該是遍歷List
,然後將訂閱者和訂閱事件綁定。沒撒好說的,跟進subscribe()
EventBus#subscribe()
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
// 獲取訂閱事件的類型,即訂閱方法中的唯一參數類型
Class eventType = subscriberMethod.eventType;
// 用訂閱者和訂閱方法構造一個Subscription對象
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
// 查找所有的訂閱了訂閱事件的訂閱者
CopyOnWriteArrayList subscriptions = subscriptionsByEventType.get(eventType);
// 沒有訂閱者訂閱過則新建個CopyOnWriteArrayList,並put進subscriptionsByEventType PS:CopyOnWriteArrayList支持並發讀寫
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
// 訂閱者List不為空,而且已經包含了newSubscription,則會拋出異常。即:訂閱者不能重復訂閱同一事件
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
int size = subscriptions.size();
// 根據訂閱者優先級,增加到訂閱者列表subscriptions的相應位置
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
// 獲取訂閱者所有訂閱事件的列表,默認為null
List> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
// 將訂閱事件添加進對應訂閱者的訂閱列表
subscribedEvents.add(eventType);
// sticky默認為false
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).
Set, Object>> entries = stickyEvents.entrySet();
for (Map.Entry, 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);
}
}
}
EventBus#Register()
其實只做了三件事:
1. 查找訂閱者所有的訂閱事件
2. 將訂閱事件作為key
,所有訂閱了此訂閱事件的訂閱者作為value
存放進subscriptionsByEventType
3. 將訂閱者作為key
,訂閱者的所有訂閱事件作為value
存放進typesBySubscriber
至此,EventBus.getDefault().register(this)
流程完畢。
Post
流程EventBus#getDefault()
獲取EventBus實例
。和Register
流程中一樣,不再贅述。
EventBus#post()
/** Posts the given event to the event bus. */
public void post(Object event) {
// 依據不同的線程獲取相應的剛初始化的PostingThreadState
PostingThreadState postingState = currentPostingThreadState.get();
List
上面代碼中currentPostingThreadState
為ThreadLocal
對象,對ThreadLocal<>
機制不了解的同學,可以查看這篇博客。下面跟進postSingleEvent()
方法。
EventBus#postSingleEvent()
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
// 獲取event的類型
Class eventClass = event.getClass();
boolean subscriptionFound = false;
// eventInheritance默認為true
if (eventInheritance) {
// 依據訂閱事件類型,將訂閱事件類型及所有父類添加進eventTypes。詳情見下文EventBus.lookupAllEventTypes()分析
List> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
// 遍歷countTypes,通過調用postSingleEventForEventType()方法通知所有訂閱者
for (int h = 0; h < countTypes; h++) {
Class clazz = eventTypes.get(h);
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
EventBus#lookupAllEventTypes()
private static List> lookupAllEventTypes(Class eventClass) {
synchronized (eventTypesCache) {
// 根據訂閱事件查找所有自身及父類,eventTypes默認為null
List> eventTypes = eventTypesCache.get(eventClass);
if (eventTypes == null) {
eventTypes = new ArrayList<>();
Class clazz = eventClass;
while (clazz != null) {
// 將訂閱事件添加進eventTypes
eventTypes.add(clazz);
// 遍歷訂閱事件的所有父類,依次添加進eventTypes
addInterfaces(eventTypes, clazz.getInterfaces());
clazz = clazz.getSuperclass();
}
// 將訂閱事件和包含訂閱事件自身及所有父類的eventTypes添加進eventTypesCache
eventTypesCache.put(eventClass, eventTypes);
}
return eventTypes;
}
}
現在假設傳遞的數據為Person
類,而Person
類實現了IPerson
接口。通過上面的分析可以得出結論:在傳遞對象(Person
)的時候,訂閱事件中參數為被傳遞對象的所有父類訂閱事件(IPerson
)也都會被調用。筆者已經驗證通過,感興趣的同學可以再驗證一下。
EventBus#postSingleEventForEventType()
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class eventClass) {
CopyOnWriteArrayList subscriptions;
synchronized (this) {
// 根據訂閱事件查找所有已經注冊過的訂閱者
// 注意:這裡第一次傳遞進來的是訂閱事件,之後會逐個傳遞進來訂閱事件的父類
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
// 參數解釋:subscription-被遍歷到的訂閱者;event-訂閱事件參數(子類);
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
在EventBus#register()
最後總結道:將訂閱事件作為key
,所有訂閱了此訂閱事件的訂閱者作為value
存放進subscriptionsByEventType
。這裡就依據訂閱事件然後查找對應所有的訂閱者。注意:由於遍歷訂閱事件參數所有父類的原因,一個訂閱事件的Post
第一次執行postToSubscription()
時,subscription
參數,遍歷時為訂閱事件的訂閱者。之後再調用postToSubscription()
時,subscription
參數都為訂閱時間父類的訂閱者。而event
參數則一直是訂閱事件中的唯一參數(最底層子類)。
EventBus#postToSubscription()
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);
}
}
看到這裡差不多可以松口氣,終於要分發調用訂閱者的訂閱事件了!寫了整整一下午,容我抽支煙再。
首先根據ThreadMode
確定分發類型。這裡以最常用的Main
為例,其余兩個Poster
同理。如果是isMainThread=true
,那麼直接調用invokeSubscriber()
,否則調用mainThreadPoster.enqueue()
。下面分別解釋這兩種情況。
EventBus#invokeSubscriber()
void invokeSubscriber(Subscription subscription, Object event) {
try {
// 反射調用
subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
} catch (InvocationTargetException e) {
handleSubscriberException(subscription, event, e.getCause());
} catch (IllegalAccessException e) {
throw new IllegalStateException("Unexpected exception", e);
}
}
沒撒好說的,直接反射調用訂閱者的訂閱事件。注意:參數event
是子類對象,就算調用訂閱事件中唯一參數是參數的父類,那麼傳遞的仍然是子類對象。筆者已經驗證,感興趣的同學可以自行驗證。然後查看HandlerPoster#enqueue()
。
HandlerPoster#enqueue()
final class HandlerPoster extends Handler {
...
void enqueue(Subscription subscription, Object event) {
// 嘗試從pendingPostPool中獲取pendingPost,沒有則直接new PendingPost
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
// 加入隊列
queue.enqueue(pendingPost);
if (!handlerActive) {
handlerActive = true;
// 發送空消息 調用handleMessage
if (!sendMessage(obtainMessage())) {
throw new EventBusException("Could not send handler message");
}
}
}
}
@Override
public void handleMessage(Message msg) {
// 已經切換到主線程
while (true) {
// 遍歷獲取queue中的PendingPost對象
PendingPost pendingPost = queue.poll();
...
// 調用eventBus.invokeSubscriber
eventBus.invokeSubscriber(pendingPost);
...
}
}
}
EventBus#invokeSubscriber()
void invokeSubscriber(PendingPost pendingPost) {
// 提取訂閱事件
Object event = pendingPost.event;
// 提取訂閱者
Subscription subscription = pendingPost.subscription;
// 釋放pendingPost
PendingPost.releasePendingPost(pendingPost);
if (subscription.active) {
// 反射調用訂閱者的訂閱事件
invokeSubscriber(subscription, event);
}
}
至此,訂閱者在相應線程調用訂閱事件完成,EventBus.getDefault().Post()
流程完畢。
EventBus#Post()
也只做了三件事
1. 根據訂閱事件在subscriptionsByEventType
中查找相應的訂閱者
2. 分發訂閱者的訂閱事件調用線程
2. 通過反射調用訂閱者的訂閱事件
unregister
流程EventBus#getDefault()
獲取EventBus實例
。和Register
流程中一樣,不再贅述。
EventBus#unregister()
public synchronized void unregister(Object subscriber) {
List> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
EventBus#unsubscribeByEventType()
private void unsubscribeByEventType(Object subscriber, Class eventType) {
List subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}
在EventBus#register()
最後總結道:
將訂閱事件作為key
,所有訂閱了此訂閱事件的訂閱者作為value
存放進subscriptionsByEventType
。
將訂閱者作為key
,訂閱者的所有訂閱事件作為value
存放進typesBySubscriber
。
現在要反注冊咯。移除相應的key
、value
即可。EventBus3.0
的使用及源碼解析到此結束,Have a nice day~
需求:通過以上例子我們修改測試數據後,拿到的View圖像是這樣的:而我們要的效果是縱坐標7.45以上與5.97以下的部分為紅色,7.45與6.43間為綠色,6.18與6.
果殼網旗下在行在微信公眾號上線了一款付費語音問答新產品——分答。 用戶在分答上可以自我介紹或描述擅長的領域,設置付費問答
你以為頭像更換很容易?或許對於用戶來講,在微信上更換一個頭像只是點擊頭像,選擇拍照或相冊,裁剪返回而已。但是對於程序員來說,要實現其實也挺吃力的(小火柴花了一個下午整理~
一、前言今天我們繼續來看破解apk的相關知識,在前一篇:Eclipse動態調試smali源碼破解apk我們今天主要來看如何使用IDA來調試Android中的native源