您的位置:首页 > 移动开发 > Android开发

事件总线EventBus Android开源库的使用

2014-07-19 18:01 537 查看
EventBus是android的一个开源库,https://github.com/greenrobot/EventBus

这是这个库在Github上的简单介绍

EventBus is an Android optimized publish/subscribe event bus. A typical use case for Android apps
is gluing Activities, Fragments, and background threads together. Conventional wiring of those elements often introduces complex and error-prone dependencies and life cycle issues. With EventBus propagating listeners through all participants (e.g. background
service -> activity -> multiple fragments or helper classes) becomes deprecated. EventBus decouples event senders and receivers and thus simplifies communication between app components. Less code, better quality. And you don't need to implement a single interface!

翻译上文:EventBus是一个Android优化的发布/订阅事件总线。一个典型的使用案例Android应用程序被胶合活动,片段,和后台线程在一起。这些元素的常规接线常常引入复杂且容易出错的依赖关系和生命周期问题。随着EventBus通过所有参与者的听众传播(如后台服务 - >活动 - >多个片段或辅助类)成为过时。 EventBus解耦事件发送器和接收器,从而简化了应用程序组件之间的通信。更少的代码,更好的质量。而你并不需要实现一个单一的界面!



下面是引用自http://duohuoteng.iteye.com/blog/2035036

eventbus 事件总线也是一个编程思想,为什么要设计EventBus了,因为他是领域驱动设计中比不可少的模块,它承担传输数据的作用,它可以解耦模块之间的耦合性。

在android开发过程中,我们总会遇到各个组件模块之间的通信,当功能点越来越多的时候,组件间的通信难免会变得混乱

我们还是已事例说明为什么要用eventbus

Java代码


假设你有A,B,C,D,E,F几个页面,

A 是列表,A->B,或者A->C

B->D->E-F

F 是发布页,

A页面从B,C,D,E返回时都是不需要刷新的,只有从F返回,A才需要刷新

这种情况不知道各位是怎么解决的

Java代码


在说个你有一个service是轮询后台服务器的查看用户消息数,当有新消息时,如果用户在msg页面,需要刷新当前页

你是通过广播机制处理吗?写起来很烦吧

Java代码


还有假设在第一个页面的左侧滑中,会显示用户的头像,用户名等信息,你是每次打开侧滑时你是一直获取数据吗,当然有人知道可以放到perference中

但是什么时候更新perference中的数据了,用户更新了各种资料,发布了什么都可以更新数据,维护这个更新也不容易吧

==========================================================================================

这里我写一下EventBus的简单使用方法:

前提要引入jar包,这个应该会吧
一、创建事件类

public class MyEvent {
private String str;

public MyEvent(String str){
this.str = str;
}

public String getString(){
return str;
}
}


二、创建订阅者,在里面对EventBus进行注册,这里我自己建了一个Activity

a、onEvent 它和ThreadModel中的PostThread对应,这个也是默认的类型,当使用这种类型时,回调函数和发起事件的函数会在同一个线程中执行

b、onEventMainThread,当使用这种类型时,回调函数会在主线程中执行,这个在Android中非常有用,因为在Android中禁止在子线程中修改UI

c、onEventBackgroundThread,当使用这种类型时,如果事件发起函数在主线程中执行,那么回调函数另启动一个子线程,如果事件发起函数在子线程执行,那么 回调函数就在这个子线程执行。

d、onEventBusAsync,当使用这种类型时,不管事件发起函数在哪里执行,都会另起一个线程去执行回调。

public class SubscriberActivity extends Activity implements OnClickListener{
Button toPublisher;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subscriber);
toPublisher = (Button)findViewById(R.id.as_toPublisher);
toPublisher.setOnClickListener(this);
textView = (TextView)findViewById(R.id.as_textView);

EventBus.getDefault().register(this);//注册</span>
}

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.as_toPublisher:</span>
<span style="white-space:pre">		</span>//跳转到另一个界面
Intent intent = new Intent(SubscriberActivity.this, PublisherActivity.class);
SubscriberActivity.this.startActivity(intent);
break;
}
}
}
<div style="text-indent: 28px;"><span style="font-size:14px;"><span style="font-family: Arial, Helvetica, sans-serif;">	</span><span style="color:#ff0000;font-family: Arial, Helvetica, sans-serif;">/*定义回调函数    可以定义四种方法</span><span style="font-family:宋体;color:#333333;line-height: 28px;">onEvent  onEventMainThread  onEventBackground  onEventBusAsync</span><span style="color:#ff0000;font-family: Arial, Helvetica, sans-serif;">*/</span></span></div><span style="font-size:14px;"><span style="color:#ff0000;">	public void onEventMainThread(MyEvent event){
textView.append(event.getString());
}</span>
}</span>


三、在PublisherActivity里, 点击按钮 , 执行要发送的事件,执行完,SubscriberActivity页面的textview会改变

<span style="font-size:14px;">public class PublisherActivity extends Activity implements OnClickListener{
<span style="white-space:pre">	</span>Button send;
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre">		</span>// TODO Auto-generated method stub
<span style="white-space:pre">		</span>super.onCreate(savedInstanceState);
<span style="white-space:pre">		</span>setContentView(R.layout.activity_publisher);
<span style="white-space:pre">		</span>send = (Button)findViewById(R.id.ap_send);
<span style="white-space:pre">		</span>send.setOnClickListener(this);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>public void onClick(View view) {
<span style="white-space:pre">		</span>// TODO Auto-generated method stub
<span style="white-space:pre">		</span>switch(view.getId()){
<span style="white-space:pre">		</span>case R.id.ap_send:
<span style="white-space:pre">			</span><span style="color:#ff0000;">EventBus.getDefault().post(new MyEvent("我是来自PublisherActivity的信息"));</span>
<span style="white-space:pre">			</span>break;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}</span>


具体的用法和想要深入理解的话,github上有Demo 大家可以自己再研究


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: