RxJava/RxAndroid : interval

简介: RxJava/RxAndroid : intervalimport android.support.v7.app.AppCompatActivity;import android.

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

......


相关文章
|
Java API
RxJava 之 ParallelFlowable
RxJava 之 ParallelFlowable
273 0
RxJava 之 ParallelFlowable
|
API Kotlin
RxJava的Single、Completable以及Maybe
RxJava的Single、Completable以及Maybe
281 0
RxJava的Single、Completable以及Maybe
RxJava/RxAndroid : doOnNext
RxJava/RxAndroid : doOnNext import android.support.v7.app.AppCompatActivity; import android.
1076 1
RxJava/RxAndroid:timer(long delay, TimeUnit unit)
RxJava/RxAndroid:timer(long delay, TimeUnit unit) timer起到定时器的作用,本例使用timer延迟3秒执行一个输出任务: package com.
1376 0
RxJava/RxAndroid:ConnectableObservable &amp; delaySubscription
RxJava/RxAndroid:ConnectableObservable & delaySubscription import android.
1160 0
RxJava/RxAndroid:ConnectableObservable &amp; replay(int bufferSize)
RxJava/RxAndroid:ConnectableObservable & replay(int bufferSize) import android.
942 0
RxJava/RxAndroid:repeatUntil
RxJava/RxAndroid:repeatUntil import android.support.v7.app.AppCompatActivity; import android.
1015 0
|
Android开发
RxJava/RxAndroid:ReplaySubject
RxJava/RxAndroid:ReplaySubject import android.support.v7.app.
860 0
RxJava/RxAndroid : takeLast
RxJava/RxAndroid : takeLast package zhangphil.book; import android.
940 0
RxJava/RxAndroid : doAfterNext
RxJava/RxAndroid : doAfterNext import android.support.v7.app.
915 0