国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

EventBus使用詳解(二)——EventBus使用進(jìn)階

2019-11-09 15:14:58
字體:
供稿:網(wǎng)友

一、概述

前一篇給大家裝簡(jiǎn)單演示了EventBus的onEventMainThread()函數(shù)的接收,其實(shí)EventBus還有另外有個(gè)不同的函數(shù),他們分別是:

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

這四種訂閱函數(shù)都是使用onEvent開頭的,它們的功能稍有不同,在介紹不同之前先介紹兩個(gè)概念:

告知觀察者事件發(fā)生時(shí)通過EventBus.post函數(shù)實(shí)現(xiàn),這個(gè)過程叫做事件的發(fā)布,觀察者被告知事件發(fā)生叫做事件的接收,是通過下面的訂閱函數(shù)實(shí)現(xiàn)的。

onEvent: 如果使用onEvent作為訂閱函數(shù),那么該事件在哪個(gè)線程發(fā)布出來的,onEvent就會(huì)在這個(gè)線程中運(yùn)行,也就是說發(fā)布事件和接收事件線程在同一個(gè)線程。使用這個(gè)方法時(shí),在onEvent方法中不能執(zhí)行耗時(shí)操作,如果執(zhí)行耗時(shí)操作容易導(dǎo)致事件分發(fā)延遲。

onEventMainThread: 如果使用onEventMainThread作為訂閱函數(shù),那么不論事件是在哪個(gè)線程中發(fā)布出來的,onEventMainThread都會(huì)在UI線程中執(zhí)行,接收事件就會(huì)在UI線程中運(yùn)行,這個(gè)在Android中是非常有用的,因?yàn)樵贏ndroid中只能在UI線程中跟新UI,所以在onEvnetMainThread方法中是不能執(zhí)行耗時(shí)操作的。

onEventBackground: 如果使用onEventBackgrond作為訂閱函數(shù),那么如果事件是在UI線程中發(fā)布出來的,那么onEventBackground就會(huì)在子線程中運(yùn)行,如果事件本來就是子線程中發(fā)布出來的,那么onEventBackground函數(shù)直接在該子線程中執(zhí)行。

onEventAsync: 使用這個(gè)函數(shù)作為訂閱函數(shù),那么無論事件在哪個(gè)線程發(fā)布,都會(huì)創(chuàng)建新的子線程在執(zhí)行onEventAsync.

二、實(shí)戰(zhàn)

1、解析

上面列出的這四個(gè)函數(shù),關(guān)鍵問題在于,我們?cè)趺粗付ㄕ{(diào)用哪個(gè)函數(shù)呢?

我們先研究一下,上一篇中是怎么調(diào)用的onEventMainThread函數(shù),除了在接收端注冊(cè)與反注冊(cè)以后,關(guān)鍵問題在于新建的一個(gè)類:

新建一個(gè)類:

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; } }

發(fā)送時(shí):

EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));

接收時(shí):

發(fā)現(xiàn)什么問題了沒? 沒錯(cuò),發(fā)送時(shí)發(fā)送的是這個(gè)類的實(shí)例,接收時(shí)參數(shù)就是這個(gè)類實(shí)例。

所以!!!!!!當(dāng)發(fā)過來一個(gè)消息的時(shí)候,EventBus怎么知道要調(diào)哪個(gè)函數(shù)呢,就看哪個(gè)函數(shù)傳進(jìn)去的參數(shù)是這個(gè)類的實(shí)例,哪個(gè)是就調(diào)哪個(gè)。那如果有兩個(gè)是呢,那兩個(gè)都會(huì)被調(diào)用!!!!

為了證明這個(gè)問題,下面寫個(gè)例子,先看下效果

2、實(shí)例

先看看我們要實(shí)現(xiàn)的效果:

這次我們?cè)谏弦黄幕A(chǔ)上,新建三個(gè)類:FirstEvent、SecondEvent、ThirdEvent,在第二個(gè)Activity中發(fā)送請(qǐng)求,在MainActivity中接收這三個(gè)類的實(shí)例,接收時(shí)的代碼為:

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()); }

使用兩個(gè)onEventMainThread分別接收FirstEvent實(shí)例的消息和SecondEvent實(shí)例的消息,使用onEvent接收ThirdEvent實(shí)例的消息。界面操作及結(jié)果如下:

這里寫圖片描述

Log輸出結(jié)果: 這里寫圖片描述

可以看到,在發(fā)送FirstEvent時(shí),在MainActiviy中雖然有三個(gè)函數(shù),但只有第一個(gè)onEventMainThread函數(shù)的接收參數(shù)是FirstEvent,所以會(huì)傳到它這來接收。所以這里識(shí)別調(diào)用EventBus中四個(gè)函數(shù)中哪個(gè)函數(shù),是通過參數(shù)中的實(shí)例來決定的。

因?yàn)槲覀兪窃谏弦黄拥幕A(chǔ)上完成的,所以這里的代碼就不詳細(xì)寫了,只寫改動(dòng)的部分。

1、三個(gè)類

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; } }

2、發(fā)送

然后在SecondActivity中新建三個(gè)按鈕,分別發(fā)送不同的類的實(shí)例,代碼如下:

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片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")); } }); } }

3、接收

在MainActivity中,除了注冊(cè)與注冊(cè),我們利用onEventMainThread(FirstEvent event)來接收來自FirstEvent的消息,使用onEventMainThread(SecondEvent event)接收來自SecondEvent 實(shí)例的消息,使用onEvent(ThirdEvent event) 來接收ThirdEvent 實(shí)例的消息。

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片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); } }

到這里,代碼就結(jié)束 了,從上面的代碼,我們可以看到,EventBus是怎么接收消息的,是根據(jù)參數(shù)中類的實(shí)例的類型的判定的,所以當(dāng)如果我們?cè)诮邮諘r(shí),同一個(gè)類的實(shí)例參數(shù)有兩個(gè)函數(shù)來接收會(huì)怎樣?答案是,這兩個(gè)函數(shù)都會(huì)執(zhí)行,下面實(shí)驗(yàn)一下: 在MainActivity中接收時(shí),我們?cè)诮邮誗econdEvent時(shí),在上面onEventMainThread基礎(chǔ)上另加一個(gè)onEventBackgroundThread和onEventAsync,即下面的代碼:

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片//SecondEvent接收函數(shù)一 public void onEventMainThread(SecondEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } //SecondEvent接收函數(shù)二 public void onEventBackgroundThread(SecondEvent event){ Log.d("harvic", "onEventBackground收到了消息:" + event.getMsg()); } //SecondEvent接收函數(shù)三 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接收函數(shù)一 public void onEventMainThread(SecondEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } //SecondEvent接收函數(shù)二 public void onEventBackgroundThread(SecondEvent event){ Log.d("harvic", "onEventBackground收到了消息:" + event.getMsg()); } //SecondEvent接收函數(shù)三 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); } }

經(jīng)過上面的分析,當(dāng)發(fā)送SecondEvent實(shí)例的消息過來的時(shí)候,這三個(gè)函數(shù)會(huì)同時(shí)接收到并各自執(zhí)行,所以當(dāng)點(diǎn)擊Second Event這個(gè)button的時(shí)候,會(huì)出現(xiàn)下面的結(jié)果:

這里寫圖片描述

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

參考文章:

《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實(shí)例與分析》

源碼下載地址:http://download.csdn.net/detail/harvic880925/8128633

轉(zhuǎn)載自:http://blog.csdn.net/harvic880925/article/details/40787203


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 措美县| 上高县| 广宗县| 紫云| 商都县| 黎平县| 浮梁县| 华安县| 瑞昌市| 新乐市| 南开区| 信丰县| 曲周县| 海晏县| 元谋县| 阜阳市| 扶风县| 柘城县| 昌黎县| 崇明县| 湖南省| 策勒县| 长宁区| 盐亭县| 南和县| 静宁县| 合作市| 海淀区| 怀远县| 诸城市| 巍山| 清远市| 平和县| 边坝县| 青岛市| 临武县| 唐山市| 屏边| 丹棱县| 云和县| 京山县|