├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── example
│ │ └── leon
│ │ └── eventbusdemo
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── leon
│ │ │ └── eventbusdemo
│ │ │ ├── MyEvent.java
│ │ │ ├── MyObject.java
│ │ │ ├── MyService.java
│ │ │ ├── MyStickyEvent.java
│ │ │ ├── PublisherActivity.java
│ │ │ ├── StickyActivity.java
│ │ │ └── SubscriberActivity.java
│ └── res
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── activity_publisher.xml
│ │ └── activity_sticky.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── leon
│ └── eventbusdemo
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # EventBus使用
2 |
3 | 开源地址:[https://github.com/greenrobot/EventBus](https://github.com/greenrobot/EventBus)
4 |
5 | 官方文档:[http://greenrobot.org/eventbus/documentation/](http://greenrobot.org/eventbus/documentation/)
6 |
7 |
8 | ## 使用步骤
9 |
10 | ### 1. 在Module的build.gradle添加依赖
11 |
12 | compile 'org.greenrobot:eventbus:3.0.0'
13 |
14 |
15 | ### 2. 创建事件
16 | public class MyEvent {
17 | public String msg;
18 | public MyEvent(String msg) {
19 | this.msg = msg;
20 | }
21 | }
22 |
23 | ### 3. 注册和反注册EventBus
24 | #### 在Activity中 ####
25 | @Override
26 | protected void onCreate(@Nullable Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_main);
29 | //注册事件总线
30 | EventBus.getDefault().register(this);
31 | }
32 |
33 |
34 | @Override
35 | protected void onDestroy() {
36 | super.onDestroy();
37 | //反注册事件总线
38 | EventBus.getDefault().unregister(this);
39 | }
40 |
41 | #### 在Fragment中 ####
42 | 1. 在onCreate中注册,在onDestory中反注册
43 | 2. 在onCreateView中注册,在onDestoryView中反注册
44 |
45 | #### 在自定义控件中 ####
46 | 在onAttachedToWindow中注册,在onDetachedFromWindow中反注册
47 |
48 | #### 普通类 ####
49 | 在类中创建注册方法和反注册方法,在合适的时机调用
50 |
51 | ### 4. 监听事件
52 |
53 | /**
54 | * POSTING线程模型:在哪个线程发布事件,就在哪个线程执行onPostingEvent方法
55 | */
56 | @Subscribe(threadMode = ThreadMode.POSTING)
57 | public void onPostingEvent(MyEvent event) {
58 | Log.d(TAG, "onPostingEvent: " + Thread.currentThread().getName());
59 | }
60 |
61 |
62 | /**
63 | * MAIN线程模型:不管是哪个线程发布事件,都在主线程执行onMainEvent方法
64 | */
65 | @Subscribe(threadMode = ThreadMode.MAIN)
66 | public void onMainEvent(MyEvent event) {
67 | Log.d(TAG, "onMainEvent: " + Thread.currentThread().getName());
68 | }
69 |
70 | /**
71 | * BACKGROUND线程模型:事件如果是在子线程发布,onBackgroundEvent方法就在该子线程执行,事件如果是在
72 | * 主线程中发布,onBackgroundEvent方法就在EventBus内部的线程池中执行
73 | */
74 | @Subscribe(threadMode = ThreadMode.BACKGROUND)
75 | public void onBackgroundEvent(MyEvent event) {
76 | Log.d(TAG, "onBackgroundEvent: " + Thread.currentThread().getName());
77 | }
78 |
79 | /**
80 | * ASYNC线程模型:不管事件在哪个线程发布,onAsyncEvent方法都在EventBus内部的线程池中执行
81 | */
82 | @Subscribe(threadMode = ThreadMode.ASYNC)
83 | public void onAsyncEvent(MyEvent event) {
84 | Log.d(TAG, "onAsyncEvent: " + Thread.currentThread().getName());
85 | }
86 |
87 | ### 5. 发布事件
88 | /**
89 | * 在主线程中发布事件
90 | * @param view
91 | */
92 | public void onPublishEventOnMainThread(View view) {
93 | MyEvent event = new MyEvent("msg from publisher main thread");
94 | EventBus.getDefault().post(event);
95 | }
96 |
97 | /**
98 | * 在子线程中发送事件
99 | * @param view
100 | */
101 | public void onPublishEventOnBGThread(View view) {
102 | new Thread(new Runnable() {
103 | @Override
104 | public void run() {
105 | MyEvent event = new MyEvent("msg from publisher bg thread");
106 | EventBus.getDefault().post(event);
107 | }
108 | }).start();
109 | }
110 |
111 | ## 优先级和事件取消 ##
112 | [Priorities and Even Cancellation](http://greenrobot.org/eventbus/documentation/priorities-and-event-cancellation/)
113 |
114 | ## 粘性事件 ##
115 | [Sticky Events](http://greenrobot.org/eventbus/documentation/configuration/sticky-events/)
116 |
117 | ## 订阅者索引 ##
118 | [Subscriber Index](http://greenrobot.org/eventbus/documentation/subscriber-index/)
119 |
120 |
121 | ## EventBus源码分析 ##
122 |
123 | ### 单例模式 ###
124 |
125 | //简单单例,当多线程时,还是会创建多个示例
126 | public static EventBus getDefault() {
127 | if (defaultInstance == null) {
128 | defaultInstance = new EventBus();
129 | }
130 | return defaultInstance;
131 | }
132 |
133 | //加锁单例,每次调用都检查是否加锁
134 | public static synchronized EventBus getDefault() {
135 | if (defaultInstance == null) {
136 | defaultInstance = new EventBus();
137 | }
138 | return defaultInstance;
139 | }
140 |
141 | //两个非空,一个加锁
142 | public static EventBus getDefault() {
143 | if (defaultInstance == null) {
144 | synchronized (EventBus.class) {
145 | if (defaultInstance == null) {
146 | defaultInstance = new EventBus();
147 | }
148 | }
149 | }
150 | return defaultInstance;
151 | }
152 |
153 |
154 | ### 注册 ###
155 |
156 | public void register(Object subscriber) {
157 | Class> subscriberClass = subscriber.getClass();
158 | //找到订阅者中所有的订阅方法
159 | List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
160 | synchronized (this) {
161 | for (SubscriberMethod subscriberMethod : subscriberMethods) {
162 | //将订阅方法记录下来保存到对应事件的订阅列表中
163 | //Map, CopyOnWriteArrayList> subscriptionsByEventType
164 | subscribe(subscriber, subscriberMethod);
165 | }
166 | }
167 | }
168 |
169 | ### 发布事件 ###
170 |
171 | public void post(Object event) {
172 | PostingThreadState postingState = currentPostingThreadState.get();
173 | List