EventBus

Android-->EventBus 3.0新版使用说明(及其使用方法)

标签: EventBusAndroidSticky
分类:

当你来到此处, 你应该已经知道了EventBus是干吗的了吧? (不知道的自行百度….)
我就不阐述了, 说说区别和使用方法吧!git

EventBus 在新版(3.0beta)中取消了原来(2.+)的使用方式:github

public void onEvent(MessageEvent event) { log(event.message); }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
public void onEventMainThread(MessageEvent event) { textField.setText(event.message); }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
public void onEventBackgroundThread(MessageEvent event){ saveToDisk(event.message); }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

变成了:json

@Subscribe(threadMode = ThreadMode.MainThread) //在ui线程执行 public void onUserEvent(UserEvent event) { }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@Subscribe(threadMode = ThreadMode.BackgroundThread) //在后台线程执行 public void onUserEvent(UserEvent event) { }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@Subscribe(threadMode = ThreadMode.Async) //强制在后台执行 public void onUserEvent(UserEvent event) { }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@Subscribe(threadMode = ThreadMode.PostThread) //默认方式, 在发送线程执行 public void onUserEvent(UserEvent event) { }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

因为官方 文档 尚未更新, 特此记录,
官方文档: https://github.com/greenrobot/EventBus/blob/master/HOWTO.md#delivery-threads-and-threadmodeside


1:EventBus 的简单使用:post

//在Activity中,注册和反注册(通用的写法) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); //第1步: 注册 } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this);//反注册 } //在注册了的Activity中,声明处理事件的方法 @Subscribe(threadMode = ThreadMode.BackgroundThread) //第2步:注册一个在后台线程执行的方法,用于接收事件 public void onUserEvent(ClassEvent event) {//参数必须是ClassEvent类型, 不然不会调用此方法 } //----------华丽的分割线--------------- //在任意地方,调用发送事件 EventBus.getDefault().post(new ClassEvent());//第3步: 发送事件 //----------华丽的分割线--------------- //在任意地方,注册事件类 public static class ClassEvent{ } //定义一个事件, 能够不包含成员变量,和成员方法 //固然你也能够定义包含成员变量的事件, 用来传递参数 public class MsgEvent { public String jsonData; public MsgEvent(String jsonData) { this.jsonData = jsonData; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

2:Sticky事件的使用
以前说的使用方法, 都是须要先注册(register), 再post,才能接受到事件;
若是你使用postSticky发送事件, 那么能够不须要先注册, 也能接受到事件.ui

首先,你可能须要声明一个方法:this

//注意,和以前的方法同样,只是多了一个 sticky = true 的属性. @Subscribe(threadMode = ThreadMode.MainThread, sticky = true) public void onEvent(MsgEvent event){ }
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

其次, 你能够在没有register的状况下:(发送事件)spa

EventBus.getDefault().postSticky(new MsgEvent("With Sticky"));
  • 1
  • 1

以后, 再注册,这样就能够收到刚刚发送的事件了:.net

EventBus.getDefault().register(this);//注册以后,立刻就能收到刚刚postSticky发送的事件
  • 1
  • 1

3:参数说明:

/** * threadMode 表示方法在什么线程执行 (Android更新UI只能在主线程, 因此若是须要操做UI, 须要设置ThreadMode.MainThread) * sticky 表示是不是一个粘性事件 (若是你使用postSticky发送一个事件,那么须要设置为true才能接受到事件) * priority 优先级 (若是有多个对象同时订阅了相同的事件, 那么优先级越高,会优先被调用.) * */ @Subscribe(threadMode = ThreadMode.MainThread, sticky = true, priority = 100) public void onEvent(MsgEvent event){ }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

源码地址: https://github.com/angcyo/EventBus3.0Demo

 

1.使用步骤:

compile 'org.greenrobot:eventbus:3.0.0'
EventBus.getDefault().register(this); //1: 注册
EventBus.getDefault().unregister(this);//反注册
//发送消息方
EventBus.getDefault().post("reflash");
//接受方:
@Subscribe //2:注册一个在后台线程执行的方法,用于接收事件
public void onEventMainThread(String event) {}遇到问题:暂无
相关文章
相关标签/搜索