关于如下方法的解释说明
1、onEvent-->之前有人的博客中写的是postThread,可能方法有改动,当然可以查看源代码
2、onEventMainThread
3、onEventBackgroundThread
4、onEventAsync
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
注册新方法,貌似这个功能也被移除了
//注册:三个参数分别是,消息订阅者(接收者),接收方法名,事件类
EventBus.getDefault().register(this,"setTextA",SetTextAEvent.class);
EventBus.getDefault().register(this,"setTextB",SetTextBEvent.class);
// EventBus.getDefault().register(this,"messageFromSecondActivity",SecondActivityEvent.class);
EventBus.getDefault().registerSticky(this, "messageFromSecondActivity", SecondActivityEvent.class);
EventBus.getDefault().register(this,"countDown",CountDownEvent.class);
/**
* 与注册对应的方法名和参数,没有后缀,默认使用PostThread模式,即跟发送事件在同一线程执行
* @param event
*/
public void setTextA(SetTextAEvent event)
{
textView.setText("A");
}
public void setTextB(SetTextBEvent event)
{
textView.setText("B");
}
public void messageFromSecondActivity(SecondActivityEvent event)
{
textView.setText(event.getText());
}
/**
* 加Async后缀,异步执行。还有MainThread和BackgroundThread,分别是在主线程(UI)执行和后台线程(单一)执行
* @param event
*/
public void countDownAsync(CountDownEvent event)
{
for(int i=event.getMax();i>0;i--)
{
Log.v("CountDown", String.valueOf(i));
SystemClock.sleep(1000);
}
}