方法 | 注释 |
---|
register | 注册 EventBus |
unregister | 解绑 EventBus |
post | 发送事件消息 |
cancelEventDelivery | 取消事件传送 |
postSticky | 发送粘性事件消息 |
removeStickyEvent | 移除指定的粘性订阅事件 |
removeAllStickyEvents | 移除所有的粘性订阅事件 |
package dev.other;
import org.greenrobot.eventbus.EventBus;
public final class EventBusUtils {
private EventBusUtils() {
}
public static void register(final Object subscriber) {
EventBus eventBus = EventBus.getDefault();
if (!eventBus.isRegistered(subscriber)) {
eventBus.register(subscriber);
}
}
public static void unregister(final Object subscriber) {
EventBus eventBus = EventBus.getDefault();
if (eventBus.isRegistered(subscriber)) {
eventBus.unregister(subscriber);
}
}
public static void post(final Object event) {
EventBus.getDefault().post(event);
}
public static void cancelEventDelivery(final Object event) {
EventBus.getDefault().cancelEventDelivery(event);
}
public static void postSticky(final Object event) {
EventBus.getDefault().postSticky(event);
}
public static <T> void removeStickyEvent(final Class<T> eventType) {
T stickyEvent = EventBus.getDefault().getStickyEvent(eventType);
if (stickyEvent != null) {
EventBus.getDefault().removeStickyEvent(stickyEvent);
}
}
public static void removeAllStickyEvents() {
EventBus.getDefault().removeAllStickyEvents();
}
}