intervalRange以一个例子说明:从1开始输出10个数据,延迟0秒执行,每隔2秒执行一次:
package zhangphil.app;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
public class MainActivity extends Activity {
private final String TAG = String.valueOf(UUID.randomUUID());
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Observable.intervalRange(1, 10, 0, 2, TimeUnit.SECONDS).subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
Log.d(TAG, String.valueOf(aLong));
}
});
}
}
运行结果输出:
03-20 12:46:30.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 1
03-20 12:46:32.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 2
03-20 12:46:34.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 3
03-20 12:46:36.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 4
03-20 12:46:38.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 5
03-20 12:46:40.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 6
03-20 12:46:42.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 7
03-20 12:46:44.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 8
03-20 12:46:46.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 9
03-20 12:46:48.029 4214-4245/? D/55907a88-b6cb-4acb-b2d8-ed9c27cc9003: 10