gradle裏面配置java
compile 'io.reactivex:rxjava:1.0.14' compile 'io.reactivex:rxandroid:1.0.1'
//部分代碼展現react
public void test(){ //建立一個觀察者 Observer<Object> observer = new Observer<Object>() { @Override public void onCompleted() { Log.i(TAG, "Completed---"); } @Override public void onError(Throwable e) { Log.i(TAG, "Error"); } @Override public void onNext(Object s) { Log.i(TAG, s.toString()); } }; //使用Observable.create()建立被觀察者 Observable observable = Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { print(subscriber); } }); //建立訂閱關係 observable.subscribe(observer); } public void print(Subscriber<? super String> subscriber){ for(int i=0; i<10000; i++){ subscriber.onNext(i + ""); } //這裏表示執行結束 subscriber.onCompleted(); }
紅色標記表示該耗時操做佔用了主線程,因此須要開啓一個子線程去執行耗時操做,這裏就不寫了。android