首页 技术 正文
技术 2022年11月17日
0 收藏 793 点赞 5,072 浏览 8509 个字

相关文章:

1、《EventBus使用详解(一)——初步使用EventBus》

2、《EventBus使用详解(二)——EventBus使用进阶》

一、概述

前一篇给大家装简单演示了EventBus的onEventMainThread()函数的接收,其实EventBus还有另外有个不同的函数,他们分别是:

1、onEvent
2、onEventMainThread
3、onEventBackgroundThread
4、onEventAsync

这四种订阅函数都是使用onEvent开头的,它们的功能稍有不同,在介绍不同之前先介绍两个概念:
告知观察者事件发生时通过EventBus.post函数实现,这个过程叫做事件的发布,观察者被告知事件发生叫做事件的接收,是通过下面的订阅函数实现的。

onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.

二、实战

1、解析

上面列出的这四个函数,关键问题在于,我们怎么指定调用哪个函数呢?

我们先研究一下,上一篇中是怎么调用的onEventMainThread函数,除了在接收端注册与反注册以后,关键问题在于新建的一个类:

新建一个类:

  1. package com.harvic.other;
  2. public class FirstEvent {
  3. private String mMsg;
  4. public FirstEvent(String msg) {
  5. // TODO Auto-generated constructor stub
  6. mMsg = msg;
  7. }
  8. public String getMsg(){
  9. return mMsg;
  10. }
  11. }

发送时:

  1. EventBus.getDefault().post(new FirstEvent(“FirstEvent btn clicked”));

接收时:

  1. public void onEventMainThread(FirstEvent event) {
  2. ……
  3. }

发现什么问题了没?

没错,发送时发送的是这个类的实例,接收时参数就是这个类实例。

所以!!!!!!当发过来一个消息的时候,EventBus怎么知道要调哪个函数呢,就看哪个函数传进去的参数是这个类的实例,哪个是就调哪个。那如果有两个是呢,那两个都会被调用!!!!

为了证明这个问题,下面写个例子,先看下效果

2、实例

先看看我们要实现的效果:

这次我们在上一篇的基础上,新建三个类:FirstEvent、SecondEvent、ThirdEvent,在第二个Activity中发送请求,在MainActivity中接收这三个类的实例,接收时的代码为:

  1. public void onEventMainThread(FirstEvent event) {
  2. Log.d(“harvic”, “onEventMainThread收到了消息:” + event.getMsg());
  3. }
  4. public void onEventMainThread(SecondEvent event) {
  5. Log.d(“harvic”, “onEventMainThread收到了消息:” + event.getMsg());
  6. }
  7. public void onEvent(ThirdEvent event) {
  8. Log.d(“harvic”, “OnEvent收到了消息:” + event.getMsg());
  9. }

使用两个onEventMainThread分别接收FirstEvent实例的消息和SecondEvent实例的消息,使用onEvent接收ThirdEvent实例的消息。界面操作及结果如下:

EventBus (二) 使用详解——EventBus使用进阶

Log输出结果:

EventBus (二) 使用详解——EventBus使用进阶


以看到,在发送FirstEvent时,在MainActiviy中虽然有三个函数,但只有第一个onEventMainThread函数的接收参数是
FirstEvent,所以会传到它这来接收。所以这里识别调用EventBus中四个函数中哪个函数,是通过参数中的实例来决定的。

因为我们是在上一篇例子的基础上完成的,所以这里的代码就不详细写了,只写改动的部分。

1、三个类

  1. package com.harvic.other;
  2. public class FirstEvent {
  3. private String mMsg;
  4. public FirstEvent(String msg) {
  5. // TODO Auto-generated constructor stub
  6. mMsg = msg;
  7. }
  8. public String getMsg(){
  9. return mMsg;
  10. }
  11. }
  1. package com.harvic.other;
  2. public class SecondEvent{
  3. private String mMsg;
  4. public SecondEvent(String msg) {
  5. // TODO Auto-generated constructor stub
  6. mMsg = “MainEvent:”+msg;
  7. }
  8. public String getMsg(){
  9. return mMsg;
  10. }
  11. }
  1. package com.harvic.other;
  2. public class ThirdEvent {
  3. private String mMsg;
  4. public ThirdEvent(String msg) {
  5. // TODO Auto-generated constructor stub
  6. mMsg = msg;
  7. }
  8. public String getMsg(){
  9. return mMsg;
  10. }
  11. }

2、发送

然后在SecondActivity中新建三个按钮,分别发送不同的类的实例,代码如下:

  1. package com.harvic.tryeventbus2;
  2. import com.harvic.other.FirstEvent;
  3. import com.harvic.other.SecondEvent;
  4. import com.harvic.other.ThirdEvent;
  5. import de.greenrobot.event.EventBus;
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. public class SecondActivity extends Activity {
  11. private Button btn_FirstEvent, btn_SecondEvent, btn_ThirdEvent;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_second);
  16. btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);
  17. btn_SecondEvent = (Button) findViewById(R.id.btn_second_event);
  18. btn_ThirdEvent = (Button) findViewById(R.id.btn_third_event);
  19. btn_FirstEvent.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. // TODO Auto-generated method stub
  23. EventBus.getDefault().post(
  24. new FirstEvent(“FirstEvent btn clicked”));
  25. }
  26. });
  27. btn_SecondEvent.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. // TODO Auto-generated method stub
  31. EventBus.getDefault().post(
  32. new SecondEvent(“SecondEvent btn clicked”));
  33. }
  34. });
  35. btn_ThirdEvent.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. // TODO Auto-generated method stub
  39. EventBus.getDefault().post(
  40. new ThirdEvent(“ThirdEvent btn clicked”));
  41. }
  42. });
  43. }
  44. }

3、接收


MainActivity中,除了注册与注册,我们利用onEventMainThread(FirstEvent
event)来接收来自FirstEvent的消息,使用onEventMainThread(SecondEvent
event)接收来自SecondEvent 实例的消息,使用onEvent(ThirdEvent event) 来接收ThirdEvent
实例的消息。

  1. package com.harvic.tryeventbus2;
  2. import com.harvic.other.FirstEvent;
  3. import com.harvic.other.SecondEvent;
  4. import com.harvic.other.ThirdEvent;
  5. import de.greenrobot.event.EventBus;
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15. public class MainActivity extends Activity {
  16. Button btn;
  17. TextView tv;
  18. EventBus eventBus;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. EventBus.getDefault().register(this);
  24. btn = (Button) findViewById(R.id.btn_try);
  25. btn.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. // TODO Auto-generated method stub
  29. Intent intent = new Intent(getApplicationContext(),
  30. SecondActivity.class);
  31. startActivity(intent);
  32. }
  33. });
  34. }
  35. public void onEventMainThread(FirstEvent event) {
  36. Log.d(“harvic”, “onEventMainThread收到了消息:” + event.getMsg());
  37. }
  38. public void onEventMainThread(SecondEvent event) {
  39. Log.d(“harvic”, “onEventMainThread收到了消息:” + event.getMsg());
  40. }
  41. public void onEvent(ThirdEvent event) {
  42. Log.d(“harvic”, “OnEvent收到了消息:” + event.getMsg());
  43. }
  44. @Override
  45. protected void onDestroy() {
  46. // TODO Auto-generated method stub
  47. super.onDestroy();
  48. EventBus.getDefault().unregister(this);
  49. }
  50. }

到这里,代码就结束 了,从上面的代码,我们可以看到,EventBus是怎么接收消息的,是根据参数中类的实例的类型的判定的,所以当如果我们在接收时,同一个类的实例参数有两个函数来接收会怎样?答案是,这两个函数都会执行,下面实验一下:

在MainActivity中接收时,我们在接收SecondEvent时,在上面onEventMainThread基础上另加一个onEventBackgroundThread和onEventAsync,即下面的代码:

  1. //SecondEvent接收函数一
  2. public void onEventMainThread(SecondEvent event) {
  3. Log.d(“harvic”, “onEventMainThread收到了消息:” + event.getMsg());
  4. }
  5. //SecondEvent接收函数二
  6. public void onEventBackgroundThread(SecondEvent event){
  7. Log.d(“harvic”, “onEventBackground收到了消息:” + event.getMsg());
  8. }
  9. //SecondEvent接收函数三
  10. public void onEventAsync(SecondEvent event){
  11. Log.d(“harvic”, “onEventAsync收到了消息:” + event.getMsg());
  12. }

完整的代码在这里:

  1. package com.harvic.tryeventbus2;
  2. import com.harvic.other.FirstEvent;
  3. import com.harvic.other.SecondEvent;
  4. import com.harvic.other.ThirdEvent;
  5. import de.greenrobot.event.EventBus;
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15. public class MainActivity extends Activity {
  16. Button btn;
  17. TextView tv;
  18. EventBus eventBus;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. EventBus.getDefault().register(this);
  24. btn = (Button) findViewById(R.id.btn_try);
  25. btn.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. // TODO Auto-generated method stub
  29. Intent intent = new Intent(getApplicationContext(),
  30. SecondActivity.class);
  31. startActivity(intent);
  32. }
  33. });
  34. }
  35. public void onEventMainThread(FirstEvent event) {
  36. Log.d(“harvic”, “onEventMainThread收到了消息:” + event.getMsg());
  37. }
  38. //SecondEvent接收函数一
  39. public void onEventMainThread(SecondEvent event) {
  40. Log.d(“harvic”, “onEventMainThread收到了消息:” + event.getMsg());
  41. }
  42. //SecondEvent接收函数二
  43. public void onEventBackgroundThread(SecondEvent event){
  44. Log.d(“harvic”, “onEventBackground收到了消息:” + event.getMsg());
  45. }
  46. //SecondEvent接收函数三
  47. public void onEventAsync(SecondEvent event){
  48. Log.d(“harvic”, “onEventAsync收到了消息:” + event.getMsg());
  49. }
  50. public void onEvent(ThirdEvent event) {
  51. Log.d(“harvic”, “OnEvent收到了消息:” + event.getMsg());
  52. }
  53. @Override
  54. protected void onDestroy() {
  55. // TODO Auto-generated method stub
  56. super.onDestroy();
  57. EventBus.getDefault().unregister(this);
  58. }
  59. }

经过上面的分析,当发送SecondEvent实例的消息过来的时候,这三个函数会同时接收到并各自执行,所以当点击Second Event这个button的时候,会出现下面的结果:

EventBus (二) 使用详解——EventBus使用进阶

好啦,这篇就到了,讲来讲去就是说一个问题:消息的接收是根据参数中的类名来决定执行哪一个的;

参考文章:

《Android解耦库EventBus的使用和源码分析》:http://blog.csdn.net/yuanzeyao/article/details/38174537

《EventBus的使用初试》:http://blog.csdn.net/pp_hdsny/article/details/14523561

《EventBusExplained 》:https://code.google.com/p/guava-libraries/wiki/EventBusExplained

《Google Guava EventBus实例与分析》

 

如果我的文章有帮到你,记得关注哦!

源码下载地址:http://download.csdn.net/detail/harvic880925/8128633

请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/40787203   谢谢!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898