关于EventBus,已经有海量博文资料。
这里记录几点eventbus-3.0.0-beta1使用时的问题。
1.准备
官方代码库:https://github.com/greenrobot/EventBus
Jar包地址:http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22de.greenrobot%22%20AND%20a%3A%22eventbus%22
新建安卓项目,将jar放入libs里
2.事件订阅者与订阅函数
直接上代码,关于 Event:事件、Subscriber:事件订阅者、Publisher:事件发布者 3个概念就不赘述了。
这里要注意的就是,先看代码:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1。Subscriber,事件订阅 EventBus.getDefault().register(this); } @Override protected void onDestroy() { super.onDestroy(); // 3。取消订阅 EventBus.getDefault().unregister(this); } // 响应控件的点击,进入新的Activity public void gotoSATY(View v) { Intent i = new Intent(this, SecondActivity.class); startActivity(i); } //--------------------------------------------------------------- /** * 2。使用@Subscribe注解的方法为 * 订阅函数 */ /** * 事件的处理 在 和事件的发送相同的进程,所以事件处理时间不应太长,不然影响事件的发送线程,而这个线程可能是UI线程 */ @Subscribe(threadMode = ThreadMode.POSTING) public void onEvent(EventNormal event) { String msg = "@Subscribe onEvent收到消息:" + event.getMsg(); Log.e("ard", msg); } // 这个和 onEventAsync 方法的参数类型一致。如果Publisher post了一个EventAsync类型的事件,那么2个订阅函数都会执行 @Subscribe(threadMode = ThreadMode.POSTING) public void onEvent(EventAsync event) { String msg = "@Subscribe onEvent收到消息:" + event.getMsg(); Log.e("ard", msg); } // 在UI线程执行,但是会按顺序执行 谁先发布那就先执行谁的 @Subscribe(threadMode = ThreadMode.MAIN_ORDERED) public void onEvent(EventOrdered event) { String msg = "@Subscribe onEvent收到消息:" + event.getMsg(); } /** * 事件的处理会在 UI线程 中执行。事件处理时间不能太长,这个不用说的,长了会ANR的 */ @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(EventMainThread event) { String msg = "@Subscribe onEventMainThread收到消息:" + event.getMsg(); Log.e("ard", msg); } /** * 如果发布者的线程不是主线程,那么就在发布线程中执行处理;否则生成一个子线程处理。 * 子线程事件会被加到一个队列中,由一个线程依次处理这些事件,如果某个事件处理时间太长,会阻塞后面的事件的派发或处理。 * 因此,虽然名字是BackgroundThread,事件处理是在后台线程,但事件处理时间还是不应该太长, */ @Subscribe(threadMode = ThreadMode.BACKGROUND) public void onEventBackgroundThread(EventBackgroundThread event) { String msg = "@Subscribe onEventBackgroundThread收到消息:" + event.getMsg(); Log.e("ard", msg); } /** * 事件处理总是在单独的子线程中执行,主要用于在后台线程中执行耗时操作,每个事件会开启一个线程(有线程池),但最好限制线程的数目。 */ @Subscribe(threadMode = ThreadMode.ASYNC) public void onEventAsync(EventAsync event) { String msg = "@Subscribe onEventAsync收到消息:" + event.getMsg(); Log.e("ard", msg); } } |
“重写”,当然不是重写,定义这4个订阅函数的时候
public void onEvent(Ojbect event)
public void onEventMainThread(Ojbect event)
public void onEventBackgroundThread(Ojbect event)
public void onEventAsync(Ojbect event)
必须要用 @Subscribe 注解,否则会报错 EventBusException: Subscriber class MainActivity and its super classes have no public methods with the @Subscribe annotation
这个“MainActivity”就是你注册订阅者的类。
事件发布
使用 EventBus.getDefault() 得到一个单例的EventBus实例,再通过post发一个event。看起来就和Handler没2样,确实如此。
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 |
public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } // 以下响应按钮的点击事件 public void sendonEventEventNormal(View v){ // Publisher,事件发布者。可以在任意线程任意位置发送事件 EventBus.getDefault().post(new EventNormal("从SA发出的 normal")); } public void sendonEventEventAsync(View v){ EventBus.getDefault().post(new EventAsync("从SA发出的 async")); } public void sendonEventMainThread(View v){ EventBus.getDefault().post(new EventMainThread("从SA发出的 main")); } public void sendonEventBackgroundThread(View v){ EventBus.getDefault().post(new EventBackgroundThread("从SA发出的 bckg")); } public void sendonEventAsync(View v){ EventBus.getDefault().post(new EventAsync("从SA发出的 async")); } } |
sendonEventEventAsync 和 sendonEventAsync 2个方法发送的都是 EventAsync ,而上一步中订阅函数里有2个正是接收的此类型event。同注释说的一样,如果发送出的event类型对应多个订阅函数的参数类型,那么就会触发多个订阅函数。
关于EventBus,知乎有言:https://www.zhihu.com/question/32179258/answer/54989242
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/1820.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设