1. 程式人生 > >Android RxJava小結

Android RxJava小結

監聽 使用 lib latest color tree lease error app

一、如何使用

在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

Android RxJava小結