compile 'org.greenrobot:eventbus:3.0.0'
//注册eventBus
@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
//取消注册
@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(IdEvent event) {
if (event != null) {
System.out.println("onEventMainThread:"+event.getId()+" "+Thread.currentThread().getName());
textView.setText(""+event.getId());
}else {
System.out.println("event:"+event);
}
}
/**
* 在后台线程中执行,如果当前线程是子线程,则会在当前线程执行,如果当前线程是主线程,则会创建一个新的子线程来执行
* @param event
*/
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onEventBackgroundThread(MessageEvent event){
System.out.println("onEventBackgroundThread::"+" "+Thread.currentThread().getName());
}
/**
* 创建一个异步线程来执行
* @param event
*/
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onEventAsync(MessageEvent event){
System.out.println("onEventAsync::"+" "+Thread.currentThread().getName());
}
/**
* 在主线程中运行
* @param event
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMain(MessageEvent event){
System.out.println("onEventMain::"+" "+Thread.currentThread().getName());
}
/**
*默认的线程模式,在当前线程下运行。如果当前线程是子线程则在子线程中,当前线程是主线程,则在主线程中执行。
* @param event
*/
@Subscribe(threadMode = ThreadMode.POSTING)
public void onEventPosting(MessageEvent event){
System.out.println("onEventPosting::"+" "+Thread.currentThread().getName());
}
EventBus.getDefault().post(new MessageEvent("你好!"));
在主线程中测试的结果
发生的是另外的实体类型
在子线程中发布消息
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
//RxAndroid的依赖包
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
//被观察者在主线程中,每1ms发送一个事件
Observable.interval(1, TimeUnit.MILLISECONDS)
//.subscribeOn(Schedulers.newThread())
//将观察者的工作放在新线程环境中
.observeOn(Schedulers.newThread())
//观察者处理每1000ms才处理一个事件
.subscribe(new Action1() {
@Override
public void call(Long aLong) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.w("TAG","---->"+aLong);
}
});
在上面的代码中,被观察者发送事件的速度是观察者处理速度的1000倍
这段代码运行之后:
...
Caused by: rx.exceptions.MissingBackpressureException
...
...
Flowable t = Flowable.create(new FlowableOnSubscribe() {
@Override
public void subscribe(FlowableEmitter e) throws Exception {
e.onNext("hello Rx2.0.");
e.onComplete();
}
}, BackpressureStrategy.BUFFER);
Subscriber subscriber = new Subscriber() {
@Override
public void onSubscribe(Subscription s) {
System.out.println("onSubscribe()");
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(Object o) {
System.out.println(""+o.toString());
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
};
除了上面这两种观察者,还有一类观察者
* Single/SingleObserver ——— 返回泛型数据的结果给观察者
* Completable/CompletableObserver ——– 返回完成的结果
* Maybe/MaybeObserver ——– 前两者的复合体
* Single为例:
Single.create(new SingleOnSubscribe() {
@Override
public void subscribe(SingleEmitter e) throws Exception {
e.onSuccess(10);
}
}).subscribe(new SingleObserver() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("onSubscribe()");
}
@Override
public void onSuccess(Integer value) {
System.out.println("onSuccess:"+value);
}
@Override
public void onError(Throwable e) {
System.out.println("onError()");
}
});
12-06 10:20:35.278 21057-21057/com.flw.rx20_demo I/System.out: onSubscribe()
12-06 10:20:35.278 21057-21057/com.flw.rx20_demo I/System.out: onSuccess:10
Completable.create(new CompletableOnSubscribe() {
@Override
public void subscribe(CompletableEmitter e) throws Exception {
System.out.println("subscribe");
e.onComplete();
}
}).subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("onSubscribe");
}
@Override
public void onComplete() {
System.out.println("onComplete");
}
@Override
public void onError(Throwable e) {
System.out.println("onError");
}
});
显示结果:
12-06 10:02:31.964 4341-4341/? I/System.out: onSubscribe
12-06 10:02:31.964 4341-4341/? I/System.out: subscribe
12-06 10:02:31.964 4341-4341/? I/System.out: onComplete
Maybe.create(new MaybeOnSubscribe() {
@Override
public void subscribe(MaybeEmitter e) throws Exception {
System.out.println("subscribe()");
e.onSuccess("hello Maybe.");
e.onComplete();
}
}).subscribe(new MaybeObserver() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("onSubscribe()");
}
@Override
public void onSuccess(String value) {
System.out.println("onSuccess():"+value);
}
@Override
public void onError(Throwable e) {
System.out.println("onError():"+e);
}
@Override
public void onComplete() {
System.out.println("onComplete()");
}
});
12-06 10:37:35.605 3344-3344/com.flw.rx20_demo I/System.out: onSubscribe()
12-06 10:37:35.605 3344-3344/com.flw.rx20_demo I/System.out: subscribe()
12-06 10:37:35.605 3344-3344/com.flw.rx20_demo I/System.out: onSuccess():hello Maybe.
subscribeOn() 指定subscribe() 所发生的线程,即 Observable.OnSubcribe 被激活时所处的线程,或者叫事件产生的线程。
observerOn() 指定Subscriber 运行所在的线程,或者叫做事件消费的线程。
// o 指的是Observable对象,在全局定义了
//相当于延迟两秒接收到被观察者发生的数据
o.timer(2,TimeUnit.SECONDS,Schedulers.newThread())
.subscribe(observer);
//从0开始递增,每隔2s递增1,一直递增至无穷大
o = Observable.interval(2,TimeUnit.SECONDS);
o.subscribe(observer);