EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息。
一、介绍 EventBus
EventBus:采用观察者设计模式,一个Android事件发布/订阅轻量级框架,由greenrobot组织贡献(该组织还贡献了greenDAO)。
通过一张图来了解下 EventBus:
参考资料:
EventBus官方网站:
https://github.com/greenrobot/EventBus
EventBus API文档:
http://greenrobot.org/files/eventbus/javadoc/3.0/
使用EventBus官方说明:
http://greenrobot.org/eventbus/documentation/how-to-get-started/
EventBus源码解析的网址:
http://www.cnblogs.com/all88/p/5338412.html
二、 EventBus的优势:
1.简化了组件间的通讯。
2.分离了事件的发送者和接受者。
3.在Activity、Fragment和线程中表现良好。
4.避免了复杂的和易错的依赖关系和声明周期问题。
5.使得代码更简洁,性能更好。
6.更快,更小(约50k的jar包)。
三、EventBus的使用方法:
1.关联EventBus框架,并新创建一个Activity。
2.完成布局及控件的初始化。
3.注册EventBus,解除EventBus注册。
4.创建EventBus消息类,设置属性。
5.使用EventBus的Post方法发送事件。
6.根据消息类,接收事件。
四、代码实战:通过一个例子来了解EventBus的强大
效果图:
步骤一:关联EventBus,Studio中快捷键为Ctrl+Shift+Alt+s
选中你所要关联的Modules,点击Dependencies,在点击+号,选择library dependency
在输入框中输入eventbus,选择greenrobot公司提供的,进行关联,并新建一个
Activity(EventBusSendActivity)
步骤二:完成页面布局以及控件初始化
MainActivity的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="35dp" android:text="由主页面跳转到发送事件的页面" android:textSize="30dp" /> <TextView android:id="@+id/txtShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button" android:layout_centerHorizontal="true" android:layout_marginTop="55dp" android:text="展示EventBus发送过来的数据" android:textSize="30dp" /> </RelativeLayout> EventBusSendActivity的布局文件:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:onClick="send" android:text="向主页面使用EventBus发送一个事件" android:textSize="30dp" /> </RelativeLayout>步骤三:在MainActivity中注册EventBus(有注册立马有销毁):EventBus.getDefault().register(this);重写onDestroy,解除EventBus注册:
@Overrideprotected void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); }步骤四:新建一个EventBus的消息类(EventBusMessage),并设置相应属性,因为我们需要
传递的消息是String类型,所以定义一个String类型的Message。
EventBusMessage完整代码:public class EventBusMessage { //定义了发送的消息是String public String Message; public EventBusMessage(String message) { Message = message; } } 步骤五:使用EventBus的Post方法发送事件,根据消息类,接收事件。完整的EventBusSendActivity代码如下:完整的MainActivity代码如下://EventBus实际上和广播是非常相似的,只不过使用更简单,代码量少 //MainActivity就是接受消息的类,你的事件在哪接受,就在哪个类进行EventBus的注册和解除操作 public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button; private TextView txtShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initView(); //1.注册EventBus,参数就是上下文 //注意:有注册EventBus就必须有解除注册的操作(一般在onDestroy方法里解除注册,防止内存泄漏,注册一个界面只能注册一次) EventBus.getDefault().register(this); } //2.在onDestroy里解除EventBus,参数是上下文 @Override protected void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); } //4.接受消息,参数就是我们定义的EventBus消息类,和发送的消息类必须是同一个,加注解 //提示:在写接受消息的方法时,方法名可以自定义,权限必须是public,参数必须是一个 @Subscribe(threadMode = ThreadMode.MAIN) public void ReciveMessage(EventBusMessage eventBusMessage){ //显示接收的消息,从这个类里拿属性 txtShow.setText(eventBusMessage.Message); } private void initView() { button = (Button) findViewById(R.id.button); button.setOnClickListener(this); txtShow = (TextView) findViewById(R.id.txtShow); txtShow.setOnClickListener(this); } //根据点击事件跳转到发送事件的Activity @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: Intent intent = new Intent(MainActivity.this,EventBusSendActivity.class); startActivity(intent); break; } } }public class EventBusSendActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event_bus_send); } //根据点击事件发送数据 public void send(View view) { //3.使用EventBus发送事件,使用Post方法,参数也必须是EventBus消息对象,且要和接受的保持一致 EventBus.getDefault().post(new EventBusMessage("你好,我是子页面,我给主页面发送一个消息")); finish(); } }按照上面的步骤即可完成效果图所实现的效果。
这样可能还体现不出EventBus的强大之处,在MainActivity的点击事件中,我们加上一个子线程,将EventBus的Post方法发送事件写在里面运行一下看看。//根据点击事件跳转到发送事件的Activity @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: //模拟子线程与主线程之间消息传递 new Thread(){ public void run() { EventBus.getDefault().post(new EventBusMessage("你好我是子页面,给你主页面发送一个消息")); }; }.start(); break; } } 运行效果:通过效果图我们可以看出来,子线程是不可以更新UI的,但是TextView展示的数据确实变了。
解释:因为注解@Subscribe(threadMode = ThreadMode.MAIN),执行设置TextView的代码是运行在主线程里,这就体现出EventBus强大之处,可以根据固定的注解自由把执行的代码放在子线程或主线程上运行。
未完待续。。。
EventBus的使用(二)注解 http://blog.youkuaiyun.com/bo543937071/article/details/53538315
EventBus的使用(三)粘性事件 http://blog.youkuaiyun.com/bo543937071/article/details/53540953