当咱们进行项目开发的时候,每每是须要应用程序的各组件、组件与后台线程间进行通讯,好比在子线程中进行请求数据,当数据请求完毕后经过Handler或者是广播通知UI,而两个Fragment之家能够经过Listener进行通讯等等。当咱们的项目愈来愈复杂,使用Intent、Handler、Broadcast进行模块间通讯、模块与后台线程进行通讯时,代码量大,并且高度耦合。如今就让咱们来学习一下EventBus 3.0吧。android
EventBus源码详解git
EventBus Github地址
进入官网,看看人家是怎么解释的:github
- simplifies the communication between components
decouples event senders and receivers
performs well with Activities, Fragments, and background threads
avoids complex and error-prone dependencies and life cycle issues- makes your code simpler
- is fast
- is tiny (~50k jar)
- is proven in practice by apps with 100,000,000+ installs
- has advanced features like delivery threads, subscriber priorities, etc.
大概的意思就是:EventBus可以简化各组件间的通讯,让咱们的代码书写变得简单,能有效的分离事件发送方和接收方(也就是解耦的意思),能避免复杂和容易出错的依赖性和生命周期问题。app
EventBus3.0有四种线程模型,分别是:ide
举个例子,我须要在一个Activity里注册EventBus事件,而后定义接收方法,这跟Android里的广播机制很像,你须要首先注册广播,而后须要编写内部类,实现接收广播,而后操做UI。因此,在EventBus中,你一样得这么作。函数
public class MessageEvent{ private String message; public MessageEvent(String message){ this.message=message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
这里有些同窗,会有一些疑问,为何要创建这样一个类,有什么用途。其实这个类就是一个Bean类,里面定义用来传输的数据的类型。post
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); }
当咱们须要在Activity或者Fragment里订阅事件时,咱们须要注册EventBus。咱们通常选择在Activity的onCreate()方法里去注册EventBus,在onDestory()方法里,去解除注册。学习
@Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }
EventBus.getDefault().post(messageEvent);
@Subscribe(threadMode = ThreadMode.MAIN) public void XXX(MessageEvent messageEvent) { ... }
前面咱们说过,处理消息的方法名字能够随便取。可是须要加一个注解@Subscribe,而且要指定线程模型。ui
所谓粘性事件,就是在发送事件以后再订阅该事件也能收到该事件。请注意这里与普通事件的区别,普通事件是先注册在绑定。
好比在项目中有这样的需求,在FirstActivity发送事件,到SecondActivity中作事件的处理。若是是使经过EventBus.getDefault.post(xx)发出的,在SecondActivity是接收不到消息的。 主要缘由是SecondActivit用于接收消息的EventBus还未完成注册,也就是发布者发了消息,但订阅者还未产生。this
EventBus.getDefault().postSticky(messageEvent);
以Sticky的形式发送的事件,在注册对象也要经过Sticky的形式进行处理事件
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void XXX(MessageEvent messageEvent) { ... }
剩下的注册操做和解绑操做和发送普通事件是同样的
以上咱们讲了EventBus的基本用法,没有用过的同窗也不要担忧不会用,小编在这里举个小栗子。
compile 'org.greenrobot:eventbus:3.0.0'
public class MessageEvent{ private String message; public MessageEvent(String message){ this.message=message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
分别在FirstActivity的onCreate()方法和onDestory()方法里,进行注册EventBus和解除注册。
package com.example.lenovo.testapp.ui; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.example.lenovo.testapp.R; import com.example.lenovo.testapp.event.MessageEvent; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; /** * Created by ZZG on 2018/1/10. */ public class FirstActivity extends AppCompatActivity { private Button mButton; private TextView mText; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_activity); mButton = (Button) findViewById(R.id.btn1); mText = (TextView) findViewById(R.id.tv1); mText.setText("今天是星期三"); EventBus.getDefault().register(this); jumpActivity(); } private void jumpActivity() { mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(FirstActivity.this,SecondActivity.class); startActivity(intent); } }); } @Subscribe(threadMode = ThreadMode.MAIN) public void Event(MessageEvent messageEvent) { mText.setText(messageEvent.getMessage()); } @Override protected void onDestroy() { super.onDestroy(); if(EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().unregister(this); } } }
在这里,事件的处理线程在主线程,是由于,我要让TextView去显示值。
在 SecondActivity里去进行事件的发送。
package com.example.lenovo.tezs; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; /** * Created by ZZG on 2018/1/10. */ public class SecondActivity extends AppCompatActivity { private Button mButton2; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); mButton2=(Button) findViewById(R.id.btn2); jumpActivity(); } private void jumpActivity() { mButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post(new MessageEvent("欢迎你们浏览我写的博客")); finish(); } }); } }
很简单,当点击按钮的时候,发送了一个事件。
在FirstActivity中,左边是一个按钮,点击以后能够跳转到SecondActivity,在按钮的右边是一个TextView,用来进行结果的验证。
这是SecondActivity,在页面的左上角,是一个按钮,当点击按钮,就会发送了一个事件,最后这个Activity就会销毁掉。
此时咱们能够看到,FirstActivity里的文字已经变成了,咱们在SecondActivity里设置的文字。
通过这个简单的例子,咱们发现EventBus使用起来是如此的方便,当咱们的代码量变得不少的时候,使用EventBus后你的逻辑很是的清晰,而且代码之间高度解耦,在进行组件、页面间通讯的时候,EventBus是一个不错的选择。