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函数,除了在接收端注册与反注册以后,关键问题在于新建的一个类:
新建一个类:
- 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;
- }
- }
发送时:
- EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
接收时:
- public void onEventMainThread(FirstEvent event) {
- ……
- }
发现什么问题了没?
没错,发送时发送的是这个类的实例,接收时参数就是这个类实例。
所以!!!!!!当发过来一个消息的时候,EventBus怎么知道要调哪个函数呢,就看哪个函数传进去的参数是这个类的实例,哪个是就调哪个。那如果有两个是呢,那两个都会被调用!!!!
为了证明这个问题,下面写个例子,先看下效果
2、实例
先看看我们要实现的效果:
这次我们在上一篇的基础上,新建三个类: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中四个函数中哪个函数,是通过参数中的实例来决定的。
因为我们是在上一篇例子的基础上完成的,所以这里的代码就不详细写了,只写改动的部分。
1、三个类
- 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、发送
然后在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"));
- }
- });
- }
- }
3、接收
在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);
- }
- }
到这里,代码就结束 了,从上面的代码,我们可以看到,EventBus是怎么接收消息的,是根据参数中类的实例的类型的判定的,所以当如果我们在接收时,同一个类的实例参数有两个函数来接收会怎样?答案是,这两个函数都会执行,下面实验一下:
在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的时候,会出现下面的结果:
好啦,这篇就到了,讲来讲去就是说一个问题:消息的接收是根据参数中的类名来决定执行哪一个的;
EventBus使用详解(二)——EventBus使用进阶的更多相关文章
- 安卓高级EventBus使用详解
我本来想写但是在网上看了下感觉写得不如此作者写得好:http://www.jianshu.com/p/da9e193e8b03 前言:EventBus出来已经有一段时间了,github上面也有很多开源 ...
- PopUpWindow使用详解(二)——进阶及答疑
相关文章:1.<PopUpWindow使用详解(一)——基本使用>2.<PopUpWindow使用详解(二)——进阶及答疑> 上篇为大家基本讲述了有关PopupWindow ...
- .NET DLL 保护措施详解(二)关于性能的测试
先说结果: 加了缓存的结果与C#原生代码差异不大了 我对三种方式进行了测试: 第一种,每次调用均动态编译 第二种,缓存编译好的对象 第三种,直接调用原生C#代码 .net dll保护系列 ------ ...
- Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)
[Android布局学习系列] 1.Android 布局学习之——Layout(布局)详解一 2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数) 3.And ...
- logback -- 配置详解 -- 二 -- <appender>
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- 爬虫入门之urllib库详解(二)
爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...
- [转]文件IO详解(二)---文件描述符(fd)和inode号的关系
原文:https://www.cnblogs.com/frank-yxs/p/5925563.html 文件IO详解(二)---文件描述符(fd)和inode号的关系 ---------------- ...
- Android View 的绘制流程之 Layout 和 Draw 过程详解 (二)
View 的绘制系列文章: Android View 的绘制流程之 Measure 过程详解 (一) Android View 绘制流程之 DecorView 与 ViewRootImpl 在上一篇 ...
- HTTPS详解二:SSL / TLS 工作原理和详细握手过程
HTTPS 详解一:附带最精美详尽的 HTTPS 原理图 HTTPS详解二:SSL / TLS 工作原理和详细握手过程 在上篇文章HTTPS详解一中,我已经为大家介绍了 HTTPS 的详细原理和通信流 ...
随机推荐
- 2016HUAS暑假集训题1 J - 迷宫问题
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- zju(2)vivi的配置编译和固化
1.实验目的 熟悉vivi的知识和应用并使用交叉编译平台vivi引导并烧写到目标板. 二.实验内容 1. 在Ubuntu下配置vivi并进行交叉编译: 2. 将编译好的vivi烧写到目标板上. 三.主 ...
- Empire C:Basic 2
作为人与计算机沟通的媒介,C语言给我们呈现了:printf.scanf.以及缓冲区. 1.printf("%d",a) %d:d是decimal base(十进制)的开头字母,意思 ...
- 【iCore3 双核心板_ uC/OS-III】例程八:互斥信号量
实验指导书及代码包下载: http://pan.baidu.com/s/1geDzqqn iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- chmod() has been disabled for security reasons
最近用 codeigniter 写一个小系统,引用了session 库,codeigniter默认的session存储方式为files.鉴于安全性,文件即肯定涉及到权限问题. 在类 UNIX 操作系统 ...
- jfinal路由简单解析
在jfinal中,通过JFinalFilter对所有的类进行过滤. 以下是路由的调用关系(我在调用关系旁边做了标记,会贴出具体的代码和解释): -1- Config: Routes -2- Inter ...
- 一个php的爬虫,将笔趣阁的书可以都下载下来。
数据库:book 表id ---- 数据库: `book`-- -- -------------------------------------------------------- ---- 表的结 ...
- Hibernate - lazy, fetch, inverse, cascade
Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了"inverse=false"(默认),那么那一 ...
- Maven-008-Nexus 私服部署发布报错 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 解决方案
我在部署构件至 maven nexus 私服时,有时会出现 Failed to deploy artifacts: Failed to transfer file: ... Return code i ...
- 解决 git 中文路径显示 unicode 代码的问题
解决 git 中文路径显示 unicode 代码的问题 当被修改的文件中带有中文字符时,中文字符会被转换为 unicode 代码,看不出原来的文件名. 这时,只要配置 :: git config -- ...