EventBus
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的更多相关文章
- Android消息传递之基于RxJava实现一个EventBus - RxBus
前言: 上篇文章学习了Android事件总线管理开源框架EventBus,EventBus的出现大大降低了开发成本以及开发难度,今天我们就利用目前大红大紫的RxJava来实现一下类似EventBus事 ...
- EventBus实现activity跟fragment交互数据
最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...
- 【热门技术】EventBus 3.0,让事件订阅更简单,从此告别组件消息传递烦恼~
一.写在前面 还在为时间接收而烦恼吗?还在为各种组件间的消息传递烦恼吗?EventBus 3.0,专注于android的发布.订阅事件总线,让各组件间的消息传递更简单!完美替代Intent,Handl ...
- 快速Android开发系列通信篇之EventBus
先吐槽一下博客园的MarkDown编辑器,推出的时候还很高兴博客园支持MarkDown了,试用了下发现支持不完善就没用了,这次这篇是在其他编辑器下写的,复制过来后发现..太烂了.怎么着作为一个技术博客 ...
- ABP源码分析二十五:EventBus
IEventData/EventData: 封装了EventData信息,触发event的源对象和时间 IEventBus/EventBus: 定义和实现了了一系列注册,注销和触发事件处理函数的方法. ...
- ABP框架 - 领域事件(EventBus)
文档目录 本节内容: EventBus 注入 IEventBus 获取默认实例 定义事件 预定义事件 处理完异常 实体修改 触发事件 处理事件 处理基类事件 处理程序异常 处理多个事件 处理程序注册 ...
- Android开发学习之路-EventBus使用
EventBus是一个通过发布.订阅事件实现组件间消息传递的工具. 它存在的目的,就是为了优化组件之间传递消息的过程.传统组件之间传递消息的方法有使用广播,回调等,而这些方法使用都比较复杂. 工作原理 ...
- Android消息传递之EventBus 3.0使用详解
前言: 前面两篇不仅学习了子线程与UI主线程之间的通信方式,也学习了如何实现组件之间通信,基于前面的知识我们今天来分析一下EventBus是如何管理事件总线的,EventBus到底是不是最佳方案?学习 ...
- Android EventBus 3.0.0 使用总结
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6039221.html 本文出自[赵彦军的博客] 前言 EventBus框架 EventBus是一个通用的叫法 ...
随机推荐
- c#根据绝对路径获取 带后缀文件名、后缀名、文件名
zz C#根据绝对路径获取 带后缀文件名.后缀名.文件名 1.c#根据绝对路径获取 带后缀文件名.后缀名.文件名. string str =" F:\test\Default.aspx& ...
- Media Queries
@media screen and (max-device-width: 1920px) and (min-device-width: 1920px) 指定1920分辨率的样式,使用device-wi ...
- [LeetCode] Ransom Note 赎金条
Given an arbitrary ransom note string and another string containing letters from all th ...
- C#调用vbs脚本实现Windows版Siri
最近新加入,把自己一些有意思的小东西分享给大家,我是一个学生,代码写得少,哪里不规范,希望大家见谅. 这事我封装好的一个类,可以直接实例化对象之后,调用"对象.Talk()"方法, ...
- 为什么要在游戏开发中使用ECS模式
http://www.richardlord.net/blog/why-use-an-entity-framework Why use an entity system framework for g ...
- MobaXterm.9.4|ssh连接工具
在狂博客中,无意发现的一款集成的远程连接工具MobaXterm.9.4,官方有个人免费版,和企业版!有连接数限制,此款为破解版.感觉还挺不错的,ssh远程连接工具! 01.主界面 02.可选的远程 ...
- Android之什么是Activity和常用的ADB命令以及Android项目结构的认识
总结一下之前学习Android的一些内容 一: Android常用的ADB命令(adb android调试桥) 1.adb devices 查看模拟器设备并重新连接. 2.adb ki ...
- LVS原理详解
一.集群简介 什么是集群 计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系统中的单个计算 ...
- P2特征(一)
很多人在提到项目的特征,肯定能说出来很多的内容,但是在英国体系下,项目的特点有哪些呢?这些特点引深的内容又有什么深度的含义. 项目具有临时性:很多人都知道项目是临时的,结束了就团队成员 ...
- JavaScript零基础学习系列五
定时器 1.定时器:设定时间,在指定的时间之后执行函数或者是程序 a.反复性定时器:var dingshiqi=Window.setInterval("函数名()",时间n[毫 ...