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. GO语言总结(5)——类型转换和类型断言

    上一篇博客介绍了Go语言的数组和切片——GO语言总结(4)——映射(Map),本篇博客介绍Go语言的类型转换和类型断言 由于Go语言不允许隐式类型转换.而类型转换和类型断言的本质,就是把一个类型转换到 ...

  2. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  3. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. 《MySQL 必知必会》读书总结

    这是 <MySQL 必知必会> 的读书总结.也是自己整理的常用操作的参考手册. 使用 MySQL 连接到 MySQL shell>mysql -u root -p Enter pas ...

  5. 高介分类:核方法与支持向量机(SVM)

        数据模型:并不是简单地二维数据,多个维度或者对象的数据聚合起来      {           persion1's attr1:value1,...,persion1's attrN:va ...

  6. .Net Core Linux centos7行—发布程序到生产环境

    实验demo现在需要发布到生产环境,发现在发布的时候要考虑到不一致的几个地方. 1.各类配置文件线下,线上不一致. 2.绑定的url不一致,可能是域名不一致,也可能是schema不一致(http,ht ...

  7. Keepalived的全局配置

    Keepalived的全局配置 默认配置文件如下: ! Configuration File for keepalived global_defs { notification_email { aca ...

  8. ajax response status list [转载]

    比较理想的解释方法应该以"状态:任务(目标)+过程+表现(或特征)"的表达模式来对这几个状态进行定义  [全文]  在<Pragmatic Ajax A Web 2.0 Pr ...

  9. 【BZOJ-4698】Sandy的卡片 后缀数组

    4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 140  Solved: 55[Submit][Stat ...

  10. HDU 1233 还是畅通工程(最小生成树)

    传送门 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...