EventBus是一款針對Android優化的發佈/訂閱事件總線。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優勢是開銷小,代碼更優雅。以及將發送者和接收者解耦。 java
一、下載EventBus的類庫二、基本使用 git
(1).Define events: github
public class MessageEvent { /* Additional fields if needed */ }(2).Prepare subscribers
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>