RxJava/RxAndroid:ConnectableObservable & delaySubscription
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observables.ConnectableObservable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private String TAG = "输出";
private DisposableObserver<Long> mDisposableObserver = new DisposableObserver<Long>() {
@Override
public void onNext(Long aLong) {
Log.d(TAG, "onNext:" + aLong);
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Observable<Long> mObservable = Observable.interval(1, TimeUnit.SECONDS);
ConnectableObservable<Long> mConnectableObservable = mObservable.publish();
mConnectableObservable.connect();
mConnectableObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.delaySubscription(5, TimeUnit.SECONDS)
.subscribeWith(mDisposableObserver);
}
}
output:
01-26 14:46:06.402 24500-24500/zhangphil.book D/输出: onNext:5
01-26 14:46:07.401 24500-24500/zhangphil.book D/输出: onNext:6
01-26 14:46:08.401 24500-24500/zhangphil.book D/输出: onNext:7
01-26 14:46:09.401 24500-24500/zhangphil.book D/输出: onNext:8
01-26 14:46:10.401 24500-24500/zhangphil.book D/输出: onNext:9
01-26 14:46:11.401 24500-24500/zhangphil.book D/输出: onNext:10
01-26 14:46:12.401 24500-24500/zhangphil.book D/输出: onNext:11
01-26 14:46:13.401 24500-24500/zhangphil.book D/输出: onNext:12
01-26 14:46:14.401 24500-24500/zhangphil.book D/输出: onNext:13
01-26 14:46:15.402 24500-24500/zhangphil.book D/输出: onNext:14
01-26 14:46:16.401 24500-24500/zhangphil.book D/输出: onNext:15
01-26 14:46:17.401 24500-24500/zhangphil.book D/输出: onNext:16
......
so,we can see ,the output from 5.