-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroid 事件总线分发库的使用.java
267 lines (175 loc) · 5.95 KB
/
Android 事件总线分发库的使用.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
事件分发处理及EventBus和Otto的简介和对比。
时间总线管理:
·将事件放到队列里,用于管理和分发
·保证应用的各个部分之间搞笑的通信及数据、事件分发
·模块间解耦
Event Bus 是一个发布/订阅的时间总线。Event Bus 模式 —— 也称为 Message Bus 或者发布者/订阅者
(publisher/subscribe) 模式 —— 可以让两个组建互相通信,但是他们之间并不互相知晓。
基于时间总线管理/订阅/分发模式的。事件相应有更多的线程选择,EventBus 可以向不同的线程中发布
事件。EventBus 支持 Sticky Event。
使用时需要先注册订阅,然后订阅者分发消息数据即可。包含4个成分:分布着,订阅者,时间,总线。
订阅者可以订阅多个时间,发送者可以发布任何事件,发布者同时也可以是订阅者。分订阅、注册、发布
取消注册等步骤。
Event ---Event--> Subscribe
Publisher ---------> Event Bus onEvent()
(事件总线) post()
---Event--> Subscribe
onEvent()
EventsBus Otto
声明事件处理的方法 采用命名规范 注解
事件继承 Yes Yes
用户继承 Yes NO
缓存最近的事件 Yes,滞留事件 Yes
时间产生者(例如编码缓存时间)NO Yes
交互线程的时间传递 Yes Yes
主线程中的时间传递 Yes NO
后台线程的时间传递 Yes NO
异步的事件传递 Yes NO
Event Bus 的基本用法
分订阅、注册、发布、取消注册。
·注册:
EventBus.getDefault().register(this);
EventBus.getDefault().register(new MyClass());
//注册:三个参数分别是,消息订阅者(接受者),接收方法名,事件类
EventBus.getDefault().register(this,"setTextA",SetTextEvent.class);
·取消注册:
EventBus.getDefault.unregister(this);
EventBus.getDefault.unregister(new MyClass());
·订阅处理数据:
public void onEventMainThread {}
public void onEvent(AnyEventType event) {}
onEventPostThread //交互
onEventBackgroundThread //后台
onEventAsync //异步
·发布:
EventBus.getDefault.postSticky(new SecondActivityEvent("Message From SecondActivity")); //发不滞留的消息
EventBus.getDefault().post(new ChangelmgEvent(1)); //直接发布
/**
*/
库:
eventbus.jar
实现效果:
一处点击发送数据,另一处或多处注册点可以及时获取更新传输过来的数据
知识点:
事件类自定义、注册,订阅事件、事件的发布、数据解析、取消注册。
/**
*/
package com.example.fangyi.eventbusdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import de.greenrobot.event.EventBus;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);//注册
}
/**
* 取消注册
*/
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 发送事件
*/
private void postData() {
String string = "我是消息";
EventBus.getDefault().post(string);
}
/**
* 默认接收数据消息事件
*/
private void Event(String string) {
}
/**
* 接收数据消息事件
* 主线程
*/
public void onEventMainThread(String string) {
}
/**
* 接收数据消息事件
* 交互
*/
public void onEventPostThread(String string) {
}
/**
* 接收数据消息事件
* 后台
*/
public void onEventBackgroundThread(String string) {
}
/**
* 接收数据消息事件
* 异步
*/
public void onEventAsync(String string) {
}
}
/**
*/
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送事件"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:textSize="20sp"/>
package com.example.fangyi.eventbusdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
public class MainActivity extends AppCompatActivity {
private TextView tv_content;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_content= (TextView) this.findViewById(R.id.tv_content);
btn_send = (Button) this.findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { //发送数据事件
MyEvent myEvent = new MyEvent();
myEvent.setType("1");
myEvent.setContent("1内容");
EventBus.getDefault().post(myEvent);
}
});
EventBus.getDefault().register(this);//当前类注册
}
public void onEventMainThread(MyEvent event) {
if (event.getType().equals("1")) {
tv_content.setText(event.getContent());
}
}
/**
* 取消注册
*/
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 接收数据
*/
public void onEvent(MyEvent event) {
if (event.getType().equals("0")) {
tv_content.setText(event.getContent());
}
}
}