EventBus 增強學習深刻了解

1、概述

前一篇給你們裝簡單演示了EventBus的onEventMainThread()函數的接收,其實EventBus還有另外有個不一樣的函數,他們分別是:php

一、onEvent
二、onEventMainThread
三、onEventBackgroundThread
四、onEventAsync
android

這四種訂閱函數都是使用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

2、實戰

一、解析

上面列出的這四個函數,關鍵問題在於,咱們怎麼指定調用哪一個函數呢?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;
	}
}

  1.  
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

《Google Guava EventBus實例與分析》

 

項目實例代碼:

    https://github.com/wangzhiyuan888/EventBusDemo

相關文章
相關標籤/搜索