天天看點

RxJava/RxAndroid : interval

RxJava/RxAndroid : interval

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.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private String TAG = "輸出";

    private DisposableObserver mDisposableObserver = new DisposableObserver<Long>() {
        @Override
        public void onNext(Long l) {
            Log.d(TAG, "onNext:" + l);
        }

        @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.interval(0, 3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(mDisposableObserver);
    }
}           

output:

01-19 18:29:04.713 12631-12631/zhangphil.book D/輸出: onNext:0
01-19 18:29:07.644 12631-12631/zhangphil.book D/輸出: onNext:1
01-19 18:29:10.645 12631-12631/zhangphil.book D/輸出: onNext:2
01-19 18:29:13.644 12631-12631/zhangphil.book D/輸出: onNext:3
01-19 18:29:16.645 12631-12631/zhangphil.book D/輸出: onNext:4
01-19 18:29:19.644 12631-12631/zhangphil.book D/輸出: onNext:5
01-19 18:29:22.644 12631-12631/zhangphil.book D/輸出: onNext:6
01-19 18:29:25.645 12631-12631/zhangphil.book D/輸出: onNext:7
01-19 18:29:28.645 12631-12631/zhangphil.book D/輸出: onNext:8
01-19 18:29:31.644 12631-12631/zhangphil.book D/輸出: onNext:9

......           

繼續閱讀