Android碎碎念 -- EventBus3.0

1、概述

EventBus是一款針對Android優化的發佈/訂閱事件總線。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優勢是開銷小,代碼更優雅。以及將發送者和接收者解耦。 java

一、下載EventBus的類庫
源碼:https://github.com/greenrobot/EventBus

二、基本使用 git

(1).Define events: github

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);


三、checking post

Gradle: 優化

compile 'org.greenrobot:eventbus:3.0.0'

Maven: this

<dependency> <groupId>org.greenrobot</groupId> <artifactId>eventbus</artifactId> <version>3.0.0</version> </dependency>
相關文章
相關標籤/搜索