EventBus是由greenrobot 组织贡献的一个Android事件发布/订阅轻量级框架。EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通讯。好比请求网络,等网络返回时经过Handler或Broadcast通知UI,两个Fragment之间须要经过Listener通讯,这些需求均可以经过EventBus实现。网络
官网地址:http://greenrobot.org/eventbus/
翻译:http://blog.csdn.net/poorkick/article/details/55099311
<p>框架</p>
compile 'org.greenrobot:eventbus:3.0.0'
EventBus的三要素ide
EventBus的四种线程模型(ThreadMode)函数
使用步骤post
注册:EventBus.getDefault().register(this);优化
解注册(为防止内存泄漏):EventBus.getDefault().unregister(this);this
构造发送消息类:spa
public class MessageEvent { public String name; public String password; public MessageEvent(String name, String password) { this.name = name; this.password = password; } }
发布消息:EventBus.getDefault().post(new MessageEvent("name","password"));.net
接收消息:能够有四种线程模型选择线程
@Subscribe(threadMode = ThreadMode.MAIN) public void messageEventBus(MessageEvent event){ tv_result.setText("name:"+event.name+" passwrod:"+event.password); }
粘性事件
以前说的使用方法,都是须要先注册(register),再post,才能接受到事件;若是你使用postSticky发送事件,那么能够不须要先注册,也能接受到事件,也就是一个延迟注册的过程。
普通的事件咱们经过post发送给EventBus,发送事后以后当前已经订阅过的方法能够收到。可是若是有些事件须要全部订阅了该事件的方法都能执行呢?例如一个Activity,要求它管理的全部Fragment都能执行某一个事件,可是当前我只初始化了3个Fragment,若是这时候经过post发送了事件,那么当前的3个Fragment固然能收到。可是这个时候又初始化了2个Fragment,那么我必须从新发送事件,这两个Fragment才能执行到订阅方法。
粘性事件就是为了解决这个问题,经过 postSticky 发送粘性事件,这个事件不会只被消费一次就消失,而是一直存在系统中,知道被 removeStickyEvent 删除掉。那么只要订阅了该粘性事件的全部方法,只要被register 的时候,就会被检测到,而且执行。订阅的方法须要添加 sticky = true 属性。
构造发送信息类:
public class StickyEvent { public String msg; public StickyEvent(String msg) { this.msg = msg; } }
发布消息:EventBus.getDefault().postSticky(new StickyEvent("我是粘性事件"));
接收消息:和以前的方法同样,只是多了一个 sticky = true 的属性。
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void onEvent(StickyEvent event){ tv_c_result.setText(event.msg); }
注册:
EventBus.getDefault().register(CActivity.this);
解注册:
EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().unregister(CActivity.class);
主线程发送事件:
自定义事件(相似定义JavaBean),包含用户的姓名和密码;
public class UserEvent { private String name; private String password; public UserEvent() { } public UserEvent(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserEvent{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
在onCreate方法中注册订阅者,在onDestroy中解注册。
public class MainActivity extends AppCompatActivity { @BindView(R.id.jump) Button mJump; @BindView(R.id.send) Button mSend; @BindView(R.id.tv_result) TextView mTvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //注册订阅者 EventBus.getDefault().register(this); } @OnClick({R.id.jump, R.id.send}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.jump: startActivity(new Intent(MainActivity.this, SecActivity.class)); break; case R.id.send: break; } } //定义处理接收的方法 @Subscribe(threadMode = ThreadMode.MAIN) public void userEventBus(UserEvent userEvent){ mTvResult.setText(userEvent.toString()); } @Override protected void onDestroy() { super.onDestroy(); //注销注册 EventBus.getDefault().unregister(this); } }
在另外一个activity中发送事件,让订阅者可以接收;
@OnClick({R.id.sendData, R.id.receive}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.sendData: //发送事件 EventBus.getDefault().post(new UserEvent("Mr.sorrow", "123456")); finish(); break; case R.id.receive: break; } }
实现结果:
<p>
发送粘性事件:
MainActivity中发送粘性事件;
case R.id.send: EventBus.getDefault().postSticky(new MessageEvent("粘性事件", "urgent")); startActivity(new Intent(MainActivity.this, SecActivity.class)); break;
SecActivity中接受注册并处理;
public class SecActivity extends AppCompatActivity { @BindView(R.id.sendData) Button mSendData; @BindView(R.id.receive) Button mReceive; @BindView(R.id.tv_receive) TextView mTvReceive; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sec); ButterKnife.bind(this); } @OnClick({R.id.sendData, R.id.receive}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.sendData: //发送事件 EventBus.getDefault().post(new UserEvent("Mr.sorrow", "123456")); finish(); break; case R.id.receive: //要接收时开始注册 EventBus.getDefault().register(SecActivity.this); break; } } //处理事件逻辑 @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void receiveEventBus(MessageEvent messageEvent) { mTvReceive.setText(messageEvent.toString()); } @Override protected void onDestroy() { super.onDestroy(); //解注册 EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().unregister(SecActivity.this); } }
实现效果
<p>
做者:Mr丶sorrow 连接:https://www.jianshu.com/p/428a5257839c 來源:简书 简书著做权归做者全部,任何形式的转载都请联系做者得到受权并注明出处。