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的時候,會出現下面的結果:



好啦,這篇就到了,講來講去就是說一個問題:消息的接收是根據參數中的類名來決定執行哪一個的;


參考文章:

《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   謝謝!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章