Skip to content

Commit 77ab854

Browse files
committed
Update README.md
1 parent 1e5efee commit 77ab854

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,44 +58,59 @@ public class YourActivity extends Activity {
5858

5959
```java
6060
public class YourActivity extends Activity {
61+
6162
// code ......
6263

6364
// 接收方法,默认的tag,执行在UI线程
6465
@Subcriber
65-
private void updateTime(String time) {
66-
Log.e("", "### update time = " + time);
66+
private void updateUser(User user) {
67+
Log.e("", "### update user name = " + user.name);
6768
}
6869

6970
// 含有my_tag,当用户post事件时,只有指定了"my_tag"的事件才会触发该函数,执行在UI线程
7071
@Subcriber(tag = "my_tag")
71-
private void updateTimeWithTag(String time) {
72-
Log.e("", "### update time with my_tag, time = " + time);
72+
private void updateUserWithTag(User user) {
73+
Log.e("", "### update user with my_tag, name = " + user.name);
7374
}
7475

7576
// 含有my_tag,当用户post事件时,只有指定了"my_tag"的事件才会触发该函数,
7677
// post函数在哪个线程执行,该函数就执行在哪个线程
7778
@Subcriber(tag = "my_tag", mode=ThreadMode.POST)
78-
private void updateTimeWithMode(String time) {
79-
Log.e("", "### update time with my_tag, time = " + time);
79+
private void updateUserWithMode(User user) {
80+
Log.e("", "### update user with my_tag, name = " + user.name);
8081
}
8182

8283
// 含有my_tag,当用户post事件时,只有指定了"my_tag"的事件才会触发该函数,执行在一个独立的线程
8384
@Subcriber(tag = "my_tag", mode = ThreadMode.ASYNC)
84-
private void updateTimeAsync(String time) {
85-
Log.e("", "### update time async , time = " + time + ", thread name = " + Thread.currentThread().getName());
85+
private void updateUserAsync(User user) {
86+
Log.e("", "### update user async , name = " + user.name + ", thread name = " + Thread.currentThread().getName());
8687
}
8788
}
88-
```
8989

90-
接收函数使用tag来标识可接收的事件类型,与BroadcastReceiver中指定action是一样的,这样可以精准的投递消息。mode可以指定目标函数执行在哪个线程,默认会执行在UI线程,方便用户更新UI。目标方法执行耗时操作时,可以设置mode为ASYNC,使之执行在子线程中。
90+
```
91+
User类大致如下 :
92+
```
93+
public class User {
94+
String name ;
95+
public User(String aName) {
96+
name = aName ;
97+
}
98+
}
99+
```
100+
101+
102+
接收函数使用tag来标识可接收的事件类型,与BroadcastReceiver中指定action是一样的,这样可以精准的投递消息。mode可以指定目标函数执行在哪个线程,默认会执行在UI线程,方便用户更新UI。目标方法执行耗时操作时,可以设置mode为ASYNC,使之执行在子线程中。
103+
91104

92105
* 3. 在其他组件,例如Activity, Fragment,Service中发布事件
93106

94107
```java
95-
EventBus.getDefault().post("what's the time now ?");
108+
109+
EventBus.getDefault().post(new User("android"));
96110

97111
// post a event with tag, the tag is like broadcast's action
98-
EventBus.getDefault().post(new Date().toLocaleString(), "my_tag");
112+
EventBus.getDefault().post(new User("mr.simple"), "my_tag");
113+
99114
```
100115
发布事件之后,注册了该事件类型的对象就会接收到响应的事件.
101116

0 commit comments

Comments
 (0)