EventBus优化发布订阅事件详解

来源:互联网 发布:程序员 多屏幕 编辑:程序博客网 时间:2024/05/16 12:20

一、概述

EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
1、下载EventBus的类库
源码:https://github.com/greenrobot/EventBus

2、基本使用

(1)自定义一个类,可以是空类,比如:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class AnyEventType {  
  2.      public AnyEventType(){}  
  3.  }  

(2)在要接收消息的页面注册:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. eventBus.register(this);  

(3)发送消息

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. eventBus.post(new AnyEventType event);  

(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void onEvent(AnyEventType event) {}  
(5)解除注册
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. eventBus.unregister(this);  
顺序就是这么个顺序,可真正让自己写,估计还是云里雾里的,下面举个例子来说明下。

首先,在EventBus中,获取实例的方法一般是采用EventBus.getInstance()来获取默认的EventBus实例,当然你也可以new一个又一个,个人感觉还是用默认的比较好,以防出错。

二、实战

先给大家看个例子:

当击btn_try按钮的时候,跳到第二个Activity,当点击第二个activity上面的First Event按钮的时候向第一个Activity发送消息,当第一个Activity收到消息后,一方面将消息Toast显示,一方面放入textView中显示。


按照下面的步骤,下面来建这个工程:

1、基本框架搭建

想必大家从一个Activity跳转到第二个Activity的程序应该都会写,这里先稍稍把两个Activity跳转的代码建起来。后面再添加EventBus相关的玩意。

MainActivity布局(activity_main.xml)

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.       
  7.     <Button   
  8.         android:id="@+id/btn_try"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="btn_bty"/>  
  12.     <TextView   
  13.         android:id="@+id/tv"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="match_parent"/>  
  16.   
  17. </LinearLayout>  
新建一个Activity,SecondActivity布局(activity_second.xml)
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context="com.harvic.try_eventbus_1.SecondActivity" >  
  7.   
  8.     <Button   
  9.         android:id="@+id/btn_first_event"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="First Event"/>  
  13.   
  14. </LinearLayout>  
MainActivity.java (点击btn跳转到第二个Activity)
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     Button btn;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.   
  10.         btn = (Button) findViewById(R.id.btn_try);  
  11.   
  12.         btn.setOnClickListener(new View.OnClickListener() {  
  13.   
  14.             @Override  
  15.             public void onClick(View v) {  
  16.                 // TODO Auto-generated method stub  
  17.                 Intent intent = new Intent(getApplicationContext(),  
  18.                         SecondActivity.class);  
  19.                 startActivity(intent);  
  20.             }  
  21.         });  
  22.     }  
  23.   
  24. }  
到这,基本框架就搭完了,下面开始按步骤使用EventBus了。

2、新建一个类FirstEvent

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.other;  
  2.   
  3. public class FirstEvent {  
  4.   
  5.     private String mMsg;  
  6.     public FirstEvent(String msg) {  
  7.         // TODO Auto-generated constructor stub  
  8.         mMsg = msg;  
  9.     }  
  10.     public String getMsg(){  
  11.         return mMsg;  
  12.     }  
  13. }  
这个类很简单,构造时传进去一个字符串,然后可以通过getMsg()获取出来。

3、在要接收消息的页面注册EventBus:

在上面的GIF图片的演示中,大家也可以看到,我们是要在MainActivity中接收发过来的消息的,所以我们在MainActivity中注册消息。

通过我们会在OnCreate()函数中注册EventBus,在OnDestroy()函数中反注册。所以整体的注册与反注册的代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.tryeventbus_simple;  
  2.   
  3. import com.harvic.other.FirstEvent;  
  4.   
  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.View;  
  11. import android.widget.Button;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     Button btn;  
  18.     TextView tv;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.                 //注册EventBus  
  25.         EventBus.getDefault().register(this);  
  26.   
  27.         btn = (Button) findViewById(R.id.btn_try);  
  28.         tv = (TextView)findViewById(R.id.tv);  
  29.   
  30.         btn.setOnClickListener(new View.OnClickListener() {  
  31.   
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 // TODO Auto-generated method stub  
  35.                 Intent intent = new Intent(getApplicationContext(),  
  36.                         SecondActivity.class);  
  37.                 startActivity(intent);  
  38.             }  
  39.         });  
  40.     }  
  41.     @Override  
  42.     protected void onDestroy(){  
  43.         super.onDestroy();  
  44.         EventBus.getDefault().unregister(this);//反注册EventBus  
  45.     }  
  46. }  

4、发送消息

发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));  

完整的SecondActivity.java的代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.tryeventbus_simple;  
  2.   
  3. import com.harvic.other.FirstEvent;  
  4.   
  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.   
  11. public class SecondActivity extends Activity {  
  12.     private Button btn_FirstEvent;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_second);  
  18.         btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);  
  19.   
  20.         btn_FirstEvent.setOnClickListener(new View.OnClickListener() {  
  21.   
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 // TODO Auto-generated method stub  
  25.                 EventBus.getDefault().post(  
  26.                         new FirstEvent("FirstEvent btn clicked"));  
  27.             }  
  28.         });  
  29.     }  
  30. }  

5、接收消息

接收消息时,我们使用EventBus中最常用的onEventMainThread()函数来接收消息,具体为什么用这个,我们下篇再讲,这里先给大家一个初步认识,要先能把EventBus用起来先。

在MainActivity中重写onEventMainThread(FirstEvent event),参数就是我们自己定义的类:

在收到Event实例后,我们将其中携带的消息取出,一方面Toast出去,一方面传到TextView中;

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void onEventMainThread(FirstEvent event) {  
  2.   
  3.     String msg = "onEventMainThread收到了消息:" + event.getMsg();  
  4.     Log.d("harvic", msg);  
  5.     tv.setText(msg);  
  6.     Toast.makeText(this, msg, Toast.LENGTH_LONG).show();  
  7. }  

完整的MainActiviy代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.tryeventbus_simple;  
  2.   
  3. import com.harvic.other.FirstEvent;  
  4.   
  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.View;  
  11. import android.widget.Button;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     Button btn;  
  18.     TextView tv;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         EventBus.getDefault().register(this);  
  26.   
  27.         btn = (Button) findViewById(R.id.btn_try);  
  28.         tv = (TextView)findViewById(R.id.tv);  
  29.   
  30.         btn.setOnClickListener(new View.OnClickListener() {  
  31.   
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 // TODO Auto-generated method stub  
  35.                 Intent intent = new Intent(getApplicationContext(),  
  36.                         SecondActivity.class);  
  37.                 startActivity(intent);  
  38.             }  
  39.         });  
  40.     }  
  41.   
  42.     public void onEventMainThread(FirstEvent event) {  
  43.   
  44.         String msg = "onEventMainThread收到了消息:" + event.getMsg();  
  45.         Log.d("harvic", msg);  
  46.         tv.setText(msg);  
  47.         Toast.makeText(this, msg, Toast.LENGTH_LONG).show();  
  48.     }  
  49.   
  50.     @Override  
  51.     protected void onDestroy(){  
  52.         super.onDestroy();  
  53.         EventBus.getDefault().unregister(this);  
  54.     }  
  55. }  

好了,到这,基本上算初步把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函数,除了在接收端注册与反注册以后,关键问题在于新建的一个类:

新建一个类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.other;  
  2.   
  3. public class FirstEvent {  
  4.   
  5.     private String mMsg;  
  6.     public FirstEvent(String msg) {  
  7.         // TODO Auto-generated constructor stub  
  8.         mMsg = msg;  
  9.     }  
  10.     public String getMsg(){  
  11.         return mMsg;  
  12.     }  
  13. }  
发送时:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));    
接收时:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void onEventMainThread(FirstEvent event) {    
  2.   
  3.     ……  
  4. }    
发现什么问题了没?

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

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

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

2、实例

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

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void onEventMainThread(FirstEvent event) {  
  2.   
  3.     Log.d("harvic""onEventMainThread收到了消息:" + event.getMsg());  
  4. }  
  5.   
  6. public void onEventMainThread(SecondEvent event) {  
  7.   
  8.     Log.d("harvic""onEventMainThread收到了消息:" + event.getMsg());  
  9. }  
  10.   
  11. public void onEvent(ThirdEvent event) {  
  12.     Log.d("harvic""OnEvent收到了消息:" + event.getMsg());  
  13. }  
使用两个onEventMainThread分别接收FirstEvent实例的消息和SecondEvent实例的消息,使用onEvent接收ThirdEvent实例的消息。界面操作及结果如下:


Log输出结果:


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

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

1、三个类

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.other;  
  2.   
  3. public class FirstEvent {  
  4.   
  5.     private String mMsg;  
  6.     public FirstEvent(String msg) {  
  7.         // TODO Auto-generated constructor stub  
  8.         mMsg = msg;  
  9.     }  
  10.     public String getMsg(){  
  11.         return mMsg;  
  12.     }  
  13. }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.other;  
  2.   
  3. public class SecondEvent{  
  4.   
  5.     private String mMsg;  
  6.     public SecondEvent(String msg) {  
  7.         // TODO Auto-generated constructor stub  
  8.         mMsg = "MainEvent:"+msg;  
  9.     }  
  10.     public String getMsg(){  
  11.         return mMsg;  
  12.     }  
  13. }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.other;  
  2.   
  3. public class ThirdEvent {  
  4.   
  5.     private String mMsg;  
  6.     public ThirdEvent(String msg) {  
  7.         // TODO Auto-generated constructor stub  
  8.         mMsg = msg;  
  9.     }  
  10.     public String getMsg(){  
  11.         return mMsg;  
  12.     }  
  13. }  

2、发送

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.tryeventbus2;  
  2.   
  3. import com.harvic.other.FirstEvent;  
  4. import com.harvic.other.SecondEvent;  
  5. import com.harvic.other.ThirdEvent;  
  6.   
  7. import de.greenrobot.event.EventBus;  
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12.   
  13. public class SecondActivity extends Activity {  
  14.     private Button btn_FirstEvent, btn_SecondEvent, btn_ThirdEvent;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_second);  
  20.         btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);  
  21.         btn_SecondEvent = (Button) findViewById(R.id.btn_second_event);  
  22.         btn_ThirdEvent = (Button) findViewById(R.id.btn_third_event);  
  23.   
  24.         btn_FirstEvent.setOnClickListener(new View.OnClickListener() {  
  25.   
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 // TODO Auto-generated method stub  
  29.                 EventBus.getDefault().post(  
  30.                         new FirstEvent("FirstEvent btn clicked"));  
  31.             }  
  32.         });  
  33.           
  34.         btn_SecondEvent.setOnClickListener(new View.OnClickListener() {  
  35.   
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 // TODO Auto-generated method stub  
  39.                 EventBus.getDefault().post(  
  40.                         new SecondEvent("SecondEvent btn clicked"));  
  41.             }  
  42.         });  
  43.   
  44.         btn_ThirdEvent.setOnClickListener(new View.OnClickListener() {  
  45.   
  46.             @Override  
  47.             public void onClick(View v) {  
  48.                 // TODO Auto-generated method stub  
  49.                 EventBus.getDefault().post(  
  50.                         new ThirdEvent("ThirdEvent btn clicked"));  
  51.   
  52.             }  
  53.         });  
  54.   
  55.     }  
  56.   
  57. }  

3、接收

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.tryeventbus2;  
  2.   
  3. import com.harvic.other.FirstEvent;  
  4. import com.harvic.other.SecondEvent;  
  5. import com.harvic.other.ThirdEvent;  
  6.   
  7. import de.greenrobot.event.EventBus;  
  8. import android.app.Activity;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13. import android.view.MenuItem;  
  14. import android.view.View;  
  15. import android.widget.Button;  
  16. import android.widget.TextView;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     Button btn;  
  21.     TextView tv;  
  22.     EventBus eventBus;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.   
  29.         EventBus.getDefault().register(this);  
  30.   
  31.         btn = (Button) findViewById(R.id.btn_try);  
  32.   
  33.         btn.setOnClickListener(new View.OnClickListener() {  
  34.   
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 // TODO Auto-generated method stub  
  38.                 Intent intent = new Intent(getApplicationContext(),  
  39.                         SecondActivity.class);  
  40.                 startActivity(intent);  
  41.             }  
  42.         });  
  43.     }  
  44.   
  45.     public void onEventMainThread(FirstEvent event) {  
  46.   
  47.         Log.d("harvic""onEventMainThread收到了消息:" + event.getMsg());  
  48.     }  
  49.   
  50.     public void onEventMainThread(SecondEvent event) {  
  51.   
  52.         Log.d("harvic""onEventMainThread收到了消息:" + event.getMsg());  
  53.     }  
  54.   
  55.     public void onEvent(ThirdEvent event) {  
  56.         Log.d("harvic""OnEvent收到了消息:" + event.getMsg());  
  57.     }  
  58.   
  59.     @Override  
  60.     protected void onDestroy() {  
  61.         // TODO Auto-generated method stub  
  62.         super.onDestroy();  
  63.         EventBus.getDefault().unregister(this);  
  64.     }  
  65. }  
到这里,代码就结束 了,从上面的代码,我们可以看到,EventBus是怎么接收消息的,是根据参数中类的实例的类型的判定的,所以当如果我们在接收时,同一个类的实例参数有两个函数来接收会怎样?答案是,这两个函数都会执行,下面实验一下:

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //SecondEvent接收函数一  
  2. public void onEventMainThread(SecondEvent event) {  
  3.   
  4.     Log.d("harvic""onEventMainThread收到了消息:" + event.getMsg());  
  5. }  
  6. //SecondEvent接收函数二  
  7. public void onEventBackgroundThread(SecondEvent event){  
  8.     Log.d("harvic""onEventBackground收到了消息:" + event.getMsg());  
  9. }  
  10. //SecondEvent接收函数三  
  11. public void onEventAsync(SecondEvent event){  
  12.     Log.d("harvic""onEventAsync收到了消息:" + event.getMsg());  
  13. }  

完整的代码在这里:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.harvic.tryeventbus2;  
  2.   
  3. import com.harvic.other.FirstEvent;  
  4. import com.harvic.other.SecondEvent;  
  5. import com.harvic.other.ThirdEvent;  
  6.   
  7. import de.greenrobot.event.EventBus;  
  8. import android.app.Activity;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13. import android.view.MenuItem;  
  14. import android.view.View;  
  15. import android.widget.Button;  
  16. import android.widget.TextView;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     Button btn;  
  21.     TextView tv;  
  22.     EventBus eventBus;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.   
  29.         EventBus.getDefault().register(this);  
  30.   
  31.         btn = (Button) findViewById(R.id.btn_try);  
  32.   
  33.         btn.setOnClickListener(new View.OnClickListener() {  
  34.   
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 // TODO Auto-generated method stub  
  38.                 Intent intent = new Intent(getApplicationContext(),  
  39.                         SecondActivity.class);  
  40.                 startActivity(intent);  
  41.             }  
  42.         });  
  43.     }  
  44.   
  45.     public void onEventMainThread(FirstEvent event) {  
  46.   
  47.         Log.d("harvic""onEventMainThread收到了消息:" + event.getMsg());  
  48.     }  
  49.   
  50.     //SecondEvent接收函数一  
  51.     public void onEventMainThread(SecondEvent event) {  
  52.   
  53.         Log.d("harvic""onEventMainThread收到了消息:" + event.getMsg());  
  54.     }  
  55.     //SecondEvent接收函数二  
  56.     public void onEventBackgroundThread(SecondEvent event){  
  57.         Log.d("harvic""onEventBackground收到了消息:" + event.getMsg());  
  58.     }  
  59.     //SecondEvent接收函数三  
  60.     public void onEventAsync(SecondEvent event){  
  61.         Log.d("harvic""onEventAsync收到了消息:" + event.getMsg());  
  62.     }  
  63.   
  64.     public void onEvent(ThirdEvent event) {  
  65.         Log.d("harvic""OnEvent收到了消息:" + event.getMsg());  
  66.     }  
  67.   
  68.     @Override  
  69.     protected void onDestroy() {  
  70.         // TODO Auto-generated method stub  
  71.         super.onDestroy();  
  72.         EventBus.getDefault().unregister(this);  
  73.     }  
  74. }  
经过上面的分析,当发送SecondEvent实例的消息过来的时候,这三个函数会同时接收到并各自执行,所以当点击Second Event这个button的时候,会出现下面的结果:



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


0 0