# eventbus(Android消息订阅发布) 使用方法





## 引入依赖环境

```java
    implementation 'org.greenrobot:eventbus:3.2.0'
```

## 创建MessageEvent类

```java
public class MessageEvent {

    public  String message;
    public MessageEvent(String message) {
        this.message = message;
    }
}
```

## 消息发送（POST）

```java
EventBus.getDefault().post(new MessageEvent(message));
```

## 消息接收（sub）

```java
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        message= event.message;
        Log.i("event-bus","messgae为"+message);
    }
```

## 监听

```java
EventBus.getDefault().register(this);//在需要使用的地方注册
EventBus.getDefault().unregister(this);//不再使用后结束注册
```

[How to get started with EventBus in 3 steps - Open Source by greenrobot](https://greenrobot.org/eventbus/documentation/how-to-get-started/)

