//EventBus Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通信。比如网络请求等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus来实现
//作为消息总线,有三个主要元素
/*
1. 事件
可以是任意类型的对象
2. 事件订阅者,接收特定的事件
所有事件订阅都都是以onEvent开头的函数,具体来说,函数的名字是onEvent,onEventMainThread,onEventBackgroundThread,onEventAsync这四个
3. 事件发布者,用于通知Subscribe有事件发生
可以在任意线程任意位置发送事件,直接调用EventBus的`post(Object)`方法,可以自己实例化EventBus对象,但一般使用默认的单例就好了:`EventBus.getDefault()`,根据post函数参数的类型,会自动调用订阅相应类型事件的函数。
*/
/*
Subscriber函数的名字只能是那4个,因为每个事件订阅函数都是和一个`ThreadMode`相关联的,ThreadMode指定了会调用的函数。
PostThread: 事件的处理和事件的发送在相同的进程,对应的函数名是onEvent
MainThread: 事件的处理会在UI线程中执行,对应的函数名是onEventMainThread
BackgroundThread: 事件的处理会在一个后台线程中执行,对应的函数名是onEventBackgroundThread 但是事件处理的时间不应该太长,否则会阻塞后面的事件的派发或处理。
Async: 事件的处理会在单独的线程中执行 主要用于后台线程中执行耗时操作,每个事件会开启一个线程(有线程池),但最好限制线程的数目
//主要看事件的处理是在哪里进行,则调用相应的事件订阅
*/
/*
使用EventBus四个步骤:
1、定义一个事件
2、注册一个订阅者
3、发布一个事件
4、接受这个事件
*/
public class MyEvent{
private static final String TAG="MyEvent";
private String msg;
public MyEvent(String msg){
this.msg= msg;
}
}
public class MainActivity extends Activity{
private static final String TAG="MainActivity";
private TextView mTextView;
private Button mButton;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button1);
mTextView = (TextView)findViewById(R.id.textView1);
EventBus.getDefault().register(this);
mButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
EventBus.getDefault().post(new MyEvent());
}
});
public void onEvent(MyEvent event){
Log.e("What","[onEvent]My Thread is "+Thread.currentThread().getName());
}
}
protected void onDestroy(){
super.onDestroy();
try{
EventBus.getDefault().unregister(this);
}catch(Throwable t){
}
}
}
public class EventMainActivity extends Activity{
private TextView mTextView;
private Button mButton;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button1);
mTextView = (TextView)findViewById(R.id.textView1);
EventBus.getDefault().register(this);
mButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
new Thread(new Runnable(){
public void run(){
EventBus.getDefault().post(new MyEvent());
}
}).start();
}
});
}
public void onEvent(MyEvent event){
Log.e("What","[onEvent] My Thread is "+Thread.currentThread().getName());
}
public void onEventMainThread(MyEvent event){
Log.e("What","[onEventMainThreadMy] Thread is "+Thread.currentThread().getName());
mTextView.setText("onEventMainThread called");
}
}
public class MyEvent{
public String what;
public MyEvent(String what){
super();
this.what= what;
}
}
public class MainActivity extends Activity{
private TextView mTextView;
private Button sendButton;
private Button queryButton;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savecInstanceState);
setContentView(R.layout.activity_main);
sendButton = (Button)findViewById(R.id.button1);
queryButton = (Button)findViewById(R.id.button2);
mTextView = (TextView)findViewById(R.id.textView1);
EventBus.getDefault().register(this);
sendEvent.setOnClickListener(new OnClickListener(){
public void onClick(View view){
for(itn i=0;i<5;i++){
EventBus.getDefault().postSticky(new MyEvent(String.valueOf(i)));
}
}
});
queryButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
MyEvent lastEvent = EventBus.getDefault().getStickyEvent(MyEvent.class);
Log.e("What","[queryEvent] last Event is "+lastEvent.what);
}
});
public void onEvent(MyEvent event){
Log.e("What","[onEvent] this Event is "+event.what);
}
}
}