@interface Subscribe分析
前面參考的代碼,存放在(use_little_demo中的 eventbus3test)
https://github.com/2954722256/use_little_demo
我們通過eventbus3的注解

用ctrl+鼠標(biāo)左鍵, 跟進(jìn)去,可以看見
@interface 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;
}
我們可以看見,和我們自己例子類似的結(jié)構(gòu)
這里多了個(gè)@Documentd, 這個(gè)也提到過,文檔相關(guān)的,我們可以忽略
簡(jiǎn)單扯淡
通過
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD})
我們可以知道,是放在方法上面使用的
(我們上一篇是放在類上面使用的,和這個(gè)不同的是,只是放在方法前面使用)
也就是上圖使用的形式
這里接口,有3個(gè)方法
我們可以發(fā)現(xiàn),對(duì)應(yīng)的一些配置,例如

其實(shí),都是通過注解傳入值的
(上一篇,我們也有對(duì)應(yīng)的傳值例子,具體可以參考)
簡(jiǎn)單復(fù)習(xí)
第一篇,我們提到過,有4中模式
也就是這里配置的threadMode()返回類型ThreadMode
我們來看一下ThreadMode類
ThreadMode
/**
* Each event handler method has a thread mode, which determines in which thread the method is to be called by EventBus.
* EventBus takes care of threading independently from the posting thread.
*
* @see EventBus#register(Object)
* @author Markus
*/
public enum ThreadMode {
/**
* Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
* implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for
* simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers
* using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
*/
POSTING,
/**
* Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is
* the main thread, event handler methods will be called directly. Event handlers using this mode must return
* quickly to avoid blocking the main thread.
*/
MAIN,
/**
* Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods
* will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single
* background thread, that will deliver all its events sequentially. Event handlers using this mode should try to
* return quickly to avoid blocking the background thread.
*/
BACKGROUND,
/**
* Event handler methods are called in a separate thread. This is always independent from the posting thread and the
* main thread. Posting events never wait for event handler methods using this mode. Event handler methods should
* use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number
* of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus
* uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
*/
ASYNC
}
我們通過配置,就可以讓 EventBus類, 拿到具體的類型了
在看一下sticky()
我們默認(rèn)不配置的時(shí)候,返回false(代碼中可以知道)
我們例子里面,有一個(gè)地方,
Activity還沒有啟動(dòng),只要配置sticky = true

再通過postSticky去傳值,就可以傳遞到?jīng)]有啟動(dòng)的Activity

priority()
我們暫時(shí)還沒有用上,
我們通過看文檔(或者對(duì)線程熟悉,名字就可以猜出來),
可以了解到和對(duì)應(yīng)的優(yōu)先級(jí)有關(guān)
這里先不扯了
簡(jiǎn)單總結(jié)
@interface Subscribe
還是挺簡(jiǎn)單的,就是3個(gè)方法,可以在配置中,傳遞3中參數(shù)
(每個(gè)都有默認(rèn)值,所以也可以不配置對(duì)應(yīng)的值)
拿到值以后,和上一篇一樣,
應(yīng)該有一個(gè)地方可以拿到對(duì)應(yīng)的值,再做處理
代碼地址
前面參考的代碼,存放在(use_little_demo中的 eventbus3test)
https://github.com/2954722256/use_little_demo
下一篇我們可以了解Eventbus3代碼分析(五):getDefault(),register和EventBusBuilder等