前一篇给你们装简单演示了EventBus的onEventMainThread()函数的接收,其实EventBus还有另外有个不一样的函数,他们分别是:php
一、onEvent
二、onEventMainThread
三、onEventBackgroundThread
四、onEventAsyncandroid
这四种订阅函数都是使用onEvent开头的,它们的功能稍有不一样,在介绍不一样以前先介绍两个概念:
告知观察者事件发生时经过EventBus.post函数实现,这个过程叫作事件的发布,观察者被告知事件发生叫作事件的接收,是经过下面的订阅函数实现的。
onEvent:若是使用onEvent做为订阅函数,那么该事件在哪一个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操做,若是执行耗时操做容易致使事件分发延迟。
onEventMainThread:若是使用onEventMainThread做为订阅函数,那么不论事件是在哪一个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是很是有用的,由于在Android中只能在UI线程中跟新UI,因此在onEvnetMainThread方法中是不能执行耗时操做的。
onEventBackground:若是使用onEventBackgrond做为订阅函数,那么若是事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,若是事件原本就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数做为订阅函数,那么不管事件在哪一个线程发布,都会建立新的子线程在执行onEventAsync.
git
上面列出的这四个函数,关键问题在于,咱们怎么指定调用哪一个函数呢?github
咱们先研究一下,上一篇中是怎么调用的onEventMainThread函数,除了在接收端注册与反注册之后,关键问题在于新建的一个类:app
新建一个类:ide
package com.harvic.other; public class FirstEvent { private String mMsg; public FirstEvent(String msg) { // TODO Auto-generated constructor stub mMsg = msg; } public String getMsg(){ return mMsg; } }
发送时:wordpress
EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
接收时:函数
public void onEventMainThread(FirstEvent event) { …… }
发现什么问题了没?源码分析
没错,发送时发送的是这个类的实例,接收时参数就是这个类实例。post
因此!!!!!!当发过来一个消息的时候,EventBus怎么知道要调哪一个函数呢,就看哪一个函数传进去的参数是这个类的实例,哪一个是就调哪一个。那若是有两个是呢,那两个都会被调用!!!!
为了证实这个问题,下面写个例子,先看下效果
先看看咱们要实现的效果:
此次咱们在上一篇的基础上,新建三个类:FirstEvent、SecondEvent、ThirdEvent,在第二个Activity中发送请求,在MainActivity中接收这三个类的实例,接收时的代码为:
public void onEventMainThread(FirstEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } public void onEventMainThread(SecondEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } public void onEvent(ThirdEvent event) { Log.d("harvic", "OnEvent收到了消息:" + event.getMsg()); }
使用两个onEventMainThread分别接收FirstEvent实例的消息和SecondEvent实例的消息,使用onEvent接收ThirdEvent实例的消息。界面操做及结果以下:
Log输出结果:
能够看到,在发送FirstEvent时,在MainActiviy中虽然有三个函数,但只有第一个onEventMainThread函数的接收参数是FirstEvent,因此会传到它这来接收。因此这里识别调用EventBus中四个函数中哪一个函数,是经过参数中的实例来决定的。
由于咱们是在上一篇例子的基础上完成的,因此这里的代码就不详细写了,只写改动的部分。
package com.harvic.other; public class FirstEvent { private String mMsg; public FirstEvent(String msg) { // TODO Auto-generated constructor stub mMsg = msg; } public String getMsg(){ return mMsg; } }
package com.harvic.other; public class SecondEvent{ private String mMsg; public SecondEvent(String msg) { // TODO Auto-generated constructor stub mMsg = "MainEvent:"+msg; } public String getMsg(){ return mMsg; } }
package com.harvic.other; public class ThirdEvent { private String mMsg; public ThirdEvent(String msg) { // TODO Auto-generated constructor stub mMsg = msg; } public String getMsg(){ return mMsg; } }
而后在SecondActivity中新建三个按钮,分别发送不一样的类的实例,代码以下:
package com.harvic.tryeventbus2; import com.harvic.other.FirstEvent; import com.harvic.other.SecondEvent; import com.harvic.other.ThirdEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class SecondActivity extends Activity { private Button btn_FirstEvent, btn_SecondEvent, btn_ThirdEvent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent = (Button) findViewById(R.id.btn_first_event); btn_SecondEvent = (Button) findViewById(R.id.btn_second_event); btn_ThirdEvent = (Button) findViewById(R.id.btn_third_event); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EventBus.getDefault().post( new FirstEvent("FirstEvent btn clicked")); } }); btn_SecondEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EventBus.getDefault().post( new SecondEvent("SecondEvent btn clicked")); } }); btn_ThirdEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EventBus.getDefault().post( new ThirdEvent("ThirdEvent btn clicked")); } }); } }
在MainActivity中,除了注册与注册,咱们利用onEventMainThread(FirstEvent event)来接收来自FirstEvent的消息,使用onEventMainThread(SecondEvent event)接收来自SecondEvent 实例的消息,使用onEvent(ThirdEvent event) 来接收ThirdEvent 实例的消息。
package com.harvic.tryeventbus2; import com.harvic.other.FirstEvent; import com.harvic.other.SecondEvent; import com.harvic.other.ThirdEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button btn; TextView tv; EventBus eventBus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); btn = (Button) findViewById(R.id.btn_try); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } public void onEventMainThread(FirstEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } public void onEventMainThread(SecondEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } public void onEvent(ThirdEvent event) { Log.d("harvic", "OnEvent收到了消息:" + event.getMsg()); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); EventBus.getDefault().unregister(this); } }
在MainActivity中接收时,咱们在接收SecondEvent时,在上面onEventMainThread基础上另加一个onEventBackgroundThread和onEventAsync,即下面的代码:
//SecondEvent接收函数一 public void onEventMainThread(SecondEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } //SecondEvent接收函数二 public void onEventBackgroundThread(SecondEvent event){ Log.d("harvic", "onEventBackground收到了消息:" + event.getMsg()); } //SecondEvent接收函数三 public void onEventAsync(SecondEvent event){ Log.d("harvic", "onEventAsync收到了消息:" + event.getMsg()); }
完整的代码在这里:
package com.harvic.tryeventbus2; import com.harvic.other.FirstEvent; import com.harvic.other.SecondEvent; import com.harvic.other.ThirdEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button btn; TextView tv; EventBus eventBus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); btn = (Button) findViewById(R.id.btn_try); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } public void onEventMainThread(FirstEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } //SecondEvent接收函数一 public void onEventMainThread(SecondEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } //SecondEvent接收函数二 public void onEventBackgroundThread(SecondEvent event){ Log.d("harvic", "onEventBackground收到了消息:" + event.getMsg()); } //SecondEvent接收函数三 public void onEventAsync(SecondEvent event){ Log.d("harvic", "onEventAsync收到了消息:" + event.getMsg()); } public void onEvent(ThirdEvent event) { Log.d("harvic", "OnEvent收到了消息:" + event.getMsg()); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); EventBus.getDefault().unregister(this); } }
通过上面的分析,当发送SecondEvent实例的消息过来的时候,这三个函数会同时接收到并各自执行,因此当点击Second Event这个button的时候,会出现下面的结果:
参考文章:
《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
项目实例代码: