EventBus (二) 使用详解——EventBus使用进阶
相关文章:
1、《EventBus使用详解(一)——初步使用EventBus》
2、《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的时候,会出现下面的结果:
好啦,这篇就到了,讲来讲去就是说一个问题:消息的接收是根据参数中的类名来决定执行哪一个的;
参考文章:
《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
如果我的文章有帮到你,记得关注哦!
源码下载地址:http://download.csdn.net/detail/harvic880925/8128633
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/40787203 谢谢!
EventBus (二) 使用详解——EventBus使用进阶的更多相关文章
- ViewPager 详解(二)---详解四大函数
前言:上篇中我们讲解了如何快速实现了一个滑动页面,但问题在于,PageAdapter必须要重写的四个函数,它们都各有什么意义,在上节的函数内部为什么要这么实现,下面我们就结合Android的API说明 ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)
本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...
- 详解C#泛型(二) 获取C#中方法的执行时间及其代码注入 详解C#泛型(一) 详解C#委托和事件(二) 详解C#特性和反射(四) 记一次.net core调用SOAP接口遇到的问题 C# WebRequest.Create 锚点“#”字符问题 根据内容来产生一个二维码
详解C#泛型(二) 一.自定义泛型方法(Generic Method),将类型参数用作参数列表或返回值的类型: void MyFunc<T>() //声明具有一个类型参数的泛型方法 { ...
- Hexo系列(二) 配置文件详解
Hexo 是一款优秀的博客框架,在使用 Hexo 搭建一个属于自己的博客网站后,我们还需要对其进行配置,使得 Hexo 更能满足自己的需求 这里所说的配置文件,是位于站点根目录下的 _config.y ...
- 【模型推理】量化实现分享二:详解 KL 对称量化算法实现
欢迎关注我的公众号 [极智视界],回复001获取Google编程规范 O_o >_< o_O O_o ~_~ o_O 大家好,我是极智视界,本文剖析一下 K ...
- EventBus (一) 使用详解——初步使用EventBus
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 前言:EventBus是上周项目中用到的,网上的文章大都一样,或者过时,有用的没几篇,经过琢磨,请教他人,也终于弄清楚点眉目,记 ...
- EventBus的使用详解,功能为在Fragment,Activity,Service,线程之间传递消息
最近跟同事用到了EventBus的使用,之前不太了解EventBus,查阅资料发现EventBus还挺好用的,用法比较简单,下面就把我看到的关于EventBus的博客分享给大家,里面介绍了很多的使用详 ...
- Struts2学习笔记二 配置详解
Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...
随机推荐
- go中操作json
package main import ( "encoding/json" "fmt" ) type Server struct { ServerName st ...
- 【PAT】1012. 数字分类 (20)
1012. 数字分类 (20) 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算 ...
- 面试题12:打印1到最大的n位数(大数问题)
面试题是关于n位整数并且没有限定n的取值范围,或者是输入任意大小的整数,那么这个题目就很可能需要考虑大数问题.字符串是一个简单的.有效的表示大数的方法 这题比较难,用递归表达全排列,数字每一位都可能是 ...
- C# 6.0 新特性 (四)
原文: 1.http://www.cnblogs.com/BoyceYang/p/3711343.html 2.http://www.cnblogs.com/lhking/p/3660182.html ...
- 飘窗原生js效果
css: .close { width: 30px; height: 20px; background: white; position: absolute; right: 0; top: 0; z- ...
- out与ref修饰符
out修饰符 定义 作用 使用注意 总结 定义 out意为output,所以被out修饰的参数叫做输出参数. 通过使用out修饰的参数,方法可以返回对应参数的值 作用 先看一个例子 定义变量: ...
- Windows 10家庭版远程桌面连接错误
由于需要操作远程服务器,经常使用“远程桌面连接”工具,一直很正常.今天突然提示 出现身份验证错误,可能是由于 CredSSP加密Oracle修正.什么情况!! 根据提示的地址https://go.mi ...
- Window 下一台机器配置三个Tomcat实例
下面我们把配置的详细过程写在下面,以供参考:(此例以配置三个Tomcat为例) 1. 下载apache-tomcat-8.0.63,下载下来的文件为apache-tomcat-8.0.63.zip. ...
- 【SQL Server】书签
书签是什么 不论表是堆结构还是段结构,可以确定的是,表中每一行都是某一页的第N行,这个某一页又是某个数据库文件的第N页,这个某个数据库文件又是构成数据 库的文件组的第N个文件,因此,数据库中的每一行, ...
- mybatis基础之一
SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE conf ...