Android碎碎念 -- EventBus3.0

一、概述

EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。

1、下载EventBus的类库
源码:https://github.com/greenrobot/EventBus

2、基本使用

(1).Define events:

public class MessageEvent { /* Additional fields if needed */ }
(2).Prepare subscribers
Register your subscriber (in your onCreate or in a constructor):
eventBus.register(this);
(3).Declare your subscribing method:
@Subscribe
public void onEvent(AnyEventType event) {/* Do something */};
(4).Post events:
eventBus.post(event);


3、checking

Gradle:

compile 'org.greenrobot:eventbus:3.0.0'

Maven:

<dependency>
    <groupId>org.greenrobot</groupId>
    <artifactId>eventbus</artifactId>
    <version>3.0.0</version>
</dependency>




你可能感兴趣的:(EventBus)