使用前 需集成rxjava
使用:
/*发送数据*/
RxBus.getInstance().post(new Bean());
//接受数据
RxBus.getInstance().toObserverable(ActivityStatus.class)
.subscribe(new Action1<Bean>() {
@Override
public void call(Bean bean) {
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
}
});
public class RxBus {
private final Subject<Object, Object> _bus;
private static class RxBusHolder{
private static final RxBus instance = new RxBus();
}
private RxBus() {
_bus = new SerializedSubject<>(PublishSubject.create());
}
public static synchronized RxBus getInstance() {
return RxBusHolder.instance;
}
public void post(Object o) {
_bus.onNext(o);
}
public <T> Observable<T> toObserverable(Class<T> eventType) {
return _bus.ofType(eventType);
}
}
网友评论