EventBus

GitHub 上的地址 https://github.com/greenrobot/EventBus

EventBus干的事情可以概括来说 别人可以根据某一个事件订阅我,但是他得去实现如果我发生了这个event,我该去怎么做。所以发生这个事件的时候,所有的订阅了这个事件的订阅者都应该去执行我实现的相应的操作。那么我怎么知道发生了这个event,什么时候去执行呢,这就是eventbus要干的事情了。
所以说Eventbus可以完成线程于线程之间的通信,某些情况下相比于handler,要好用的的多,也更简单。

先介绍一下EventBus的用法:

首先你需要定义一个Event,这个是一个class, 可以随便定义,这个class就是一个事件,你可能需要这个事件去携带你想要告诉别人的信息的事件,
执行者就需要根据你这个事件来执行一些定义的操作。

你还需要一个执行者,就是注册了这个事件的执行者,和一个通知者。

<1>

public class FirstEvent {

private String content;
public FirstEvent(String content)
{
this.content = content;
} public String getContent() {
return content;
}
}

这只是一个单纯的event,没有实际的意义

<2>

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.main_button);
EventBus.getDefault().register(this);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void changeTextView(FirstEvent event) {


String msg = "changeTextView:" + event.getContent();
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

 

订阅者,EventBus.getDefault().register(this);表示该类进行了注册

最关键的是 changeTextView()这个方法,上面加上注解@Subcrib 表示当FirstEvent发生的时候 才能去执行这个方法,ThreadMode.MAIN 表示在主线程中执行。
<3>

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
button = (Button) findViewById(second_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new FirstEvent("Hello world!"));
}
});
}

发送者,这是另个activity,当你一点击button ,就会EventBus.getDefault().post(new FirstEvent("Hello world!")); 订阅者就会去执行changeTextView()这个方法。

其实EventBus的原理,就是当你注册的时候,会去得到你所有添加了@Subscribe的方法,并以event为key,以封装的method的list为Value,放到一个map中,当postevent的时候,将event根据优先级放到一个队列中,然后从队列中拿出event,去map中找到所以订阅了这个event的Method,最后根据反射去执行这个method。

在添加注解@Subscribe(threadMode = ThreadMode.MAIN) 其中有个mode 这个是一个枚举值

/**
* Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
* implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for
* simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers
* using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
*/
POSTING, //默认值。表示posting是哪个线程,就在哪个线程中执行

/**
* Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is
* the main thread, event handler methods will be called directly. Event handlers using this mode must return
* quickly to avoid blocking the main thread.
*/
MAIN,// 主线程

/**
* Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods
* will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single
* background thread, that will deliver all its events sequentially. Event handlers using this mode should try to
* return quickly to avoid blocking the background thread.
*/
BACKGROUND, //如果posting是一个主线程 就开启一个单独的线程

/**
* Event handler methods are called in a separate thread. This is always independent from the posting thread and the
* main thread. Posting events never wait for event handler methods using this mode. Event handler methods should
* use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number
* of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus
* uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
*/
ASYNC

基本用法就这么多

EventBus的更多相关文章

  1. Android消息传递之基于RxJava实现一个EventBus - RxBus

    前言: 上篇文章学习了Android事件总线管理开源框架EventBus,EventBus的出现大大降低了开发成本以及开发难度,今天我们就利用目前大红大紫的RxJava来实现一下类似EventBus事 ...

  2. EventBus实现activity跟fragment交互数据

    最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...

  3. 【热门技术】EventBus 3.0,让事件订阅更简单,从此告别组件消息传递烦恼~

    一.写在前面 还在为时间接收而烦恼吗?还在为各种组件间的消息传递烦恼吗?EventBus 3.0,专注于android的发布.订阅事件总线,让各组件间的消息传递更简单!完美替代Intent,Handl ...

  4. 快速Android开发系列通信篇之EventBus

    先吐槽一下博客园的MarkDown编辑器,推出的时候还很高兴博客园支持MarkDown了,试用了下发现支持不完善就没用了,这次这篇是在其他编辑器下写的,复制过来后发现..太烂了.怎么着作为一个技术博客 ...

  5. ABP源码分析二十五:EventBus

    IEventData/EventData: 封装了EventData信息,触发event的源对象和时间 IEventBus/EventBus: 定义和实现了了一系列注册,注销和触发事件处理函数的方法. ...

  6. ABP框架 - 领域事件(EventBus)

    文档目录 本节内容: EventBus 注入 IEventBus 获取默认实例 定义事件 预定义事件 处理完异常 实体修改 触发事件 处理事件 处理基类事件 处理程序异常 处理多个事件 处理程序注册 ...

  7. Android开发学习之路-EventBus使用

    EventBus是一个通过发布.订阅事件实现组件间消息传递的工具. 它存在的目的,就是为了优化组件之间传递消息的过程.传统组件之间传递消息的方法有使用广播,回调等,而这些方法使用都比较复杂. 工作原理 ...

  8. Android消息传递之EventBus 3.0使用详解

    前言: 前面两篇不仅学习了子线程与UI主线程之间的通信方式,也学习了如何实现组件之间通信,基于前面的知识我们今天来分析一下EventBus是如何管理事件总线的,EventBus到底是不是最佳方案?学习 ...

  9. Android EventBus 3.0.0 使用总结

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6039221.html 本文出自[赵彦军的博客] 前言 EventBus框架 EventBus是一个通用的叫法 ...

随机推荐

  1. ie6支持最小高度

    min-height:100px; height:auto !important; height:100px;

  2. UOJ #221 【NOI2016】 循环之美

    题目链接:循环之美 这道题感觉非常优美--能有一个这么优美的题面和较高的思维难度真的不容易-- 为了表示方便,让我先讲一下两个符号.\([a]\)表示如果\(a\)为真,那么返回\(1\),否则返回\ ...

  3. Angularjs+node+Mysql实现地图上的多点标注

    注:本文适合对于node有一定基础的人,如果您是小白,请先用1个小时学习node.node文档https://nodejs.org/en/docs/ 该片博文的源码地址:https://github. ...

  4. IOS比较常用的第三方组件及应用源代码(持续更新中)

    把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新~ 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com ...

  5. LVS原理详解

    一.集群简介 什么是集群 计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系统中的单个计算 ...

  6. List [][]

    # -*- coding:utf-8 -*-   L = [     ['Apple', 'Google', 'Microsoft'],     ['Java', 'Python', 'Ruby', ...

  7. ACM提交结果简介

    如果你看到红色的"Accepted",那么,恭喜你,你已经成功的解决了该问题! 如果你收到的是如下的信息,则还需要继续检查你的程序: Wrong Answer (WA) : 输出结 ...

  8. 利用django创建一个投票网站(三)

    创建你的第一个 Django 项目, 第三部分 这一篇从第二部分(zh)结尾的地方继续讲起.我们将继续编写投票应用,并且聚焦于如何创建公用界面--也被称为"视图". 设计哲学 Dj ...

  9. MVC 总结

    以下内容摘自 PHP for Absolute Beginners, Thomas Blom Hansen & Jason Lengstorf The model-view-controlle ...

  10. python中文编码问题

    第一步:在代码中输入以下命令,执行: #在Python中显示中文注释和输出中文a ="中文"print a 返回错误: d:\Python27\python.exe "D ...