Android RxJava小结
一、如何使用
在build.gradle中添加依赖
1 dependencies { 2 api 'io.reactivex:rxandroid:1.2.1' 3 api 'io.reactivex:rxjava:1.3.0' 4 implementation fileTree(include: ['*.jar'], dir: 'libs') 5 implementation 'com.android.support:appcompat-v7:27.1.1' 6 implementation 'com.android.support.constraint:constraint-layout:1.1.3' 7 testImplementation 'junit:junit:4.12' 8 androidTestImplementation 'com.android.support.test:runner:1.0.2' 9 androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 10 }
这里有一个小坑,直接用latest.release没有办法用,不知道为什么
二、代码实现
2.1 使用just+Action1+Action0来实现
1 Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 2 .subscribeOn(Schedulers.io()) // Observable运行的线程 3 .observeOn(AndroidSchedulers.mainThread()) //监听者运行的线程 4 .subscribe(new Action1<Integer>() { 5 //onNext 6 @Override 7 public void call(Integer integer) { 8 log("call 1:" + integer); 9 } 10 }, new Action1<Throwable>() { 11 //onError 12 @Override 13 public void call(Throwable throwable) { 14 log("call 2"); 15 } 16 }, new Action0() { 17 //onCompleted 18 @Override 19 public void call() { 20 log("call 3"); 21 } 22 });
运行结果如下,很简单,就不一一解释了。
1 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:1 2 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:2 3 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:3 4 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:4 5 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:5 6 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:6 7 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:7 8 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:8 9 10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 1:9 10 10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 1:10 11 10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 3
2.2 使用Just+Subscriber来实现
代码如下:
Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .subscribeOn(Schedulers.io()) // Observable运行的线程 .observeOn(AndroidSchedulers.mainThread()) //监听者运行的线程 .subscribe(new Subscriber<Integer>() { @Override public void onCompleted() { log("onCompleted"); } @Override public void onError(Throwable e) { log("onError"); } @Override public void onNext(Integer integer) { log("onNext:" + integer); } });
运行结果如下:
1 10-05 19:56:09.991 982 982 E MainActivity: yanlog msg:onNext:1 2 10-05 19:56:09.991 982 982 E MainActivity: yanlog msg:onNext:2 3 10-05 19:56:09.991 982 982 E MainActivity: yanlog msg:onNext:3 4 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:4 5 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:5 6 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:6 7 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:7 8 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:8 9 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:9 10 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:10 11 10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onCompleted