Interval每隔一定时间发射一个整数,从0开始,例如:
package zhangphil.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test();
}
private void test() {
mCompositeDisposable.add(Observable.interval(0, 5, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(getObserver(1)));
}
private DisposableObserver getObserver(final int id) {
DisposableObserver disposableObserver = new DisposableObserver<Object>() {
@Override
public void onNext(Object o) {
Log.d(id + TAG, "#####开始#####");
Log.d(id + "数据", String.valueOf(o));
Log.d(id + TAG, "#####结束#####");
}
@Override
public void onComplete() {
Log.d(id + TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(id + TAG, e.toString(), e);
}
};
return disposableObserver;
}
@Override
protected void onDestroy() {
super.onDestroy();
// 如果退出程序,就清除后台任务
mCompositeDisposable.clear();
}
}
代码输出:
05-18 17:10:58.436 8466-8466/? D/1MainActivity: #####开始#####
05-18 17:10:58.436 8466-8466/? D/1数据: 0
05-18 17:10:58.436 8466-8466/? D/1MainActivity: #####结束#####
05-18 17:10:58.439 8466-8466/? W/Activity: AppLock checkAppLockState locked:false verifying:false pkgName = zhangphil.app isInMultiWindowMode:false
05-18 17:10:58.457 8466-8502/? I/Adreno: QUALCOMM build : 5451aae, I25dc76dc3f
Build Date : 01/02/17
OpenGL ES Shader Compiler Version: XE031.09.00.03
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.UM.5.5.R1.07.00.00.269.013
Remote Branch : NONE
Reconstruct Branch : NOTHING
05-18 17:10:58.478 8466-8502/? I/OpenGLRenderer: Initialized EGL, version 1.4
05-18 17:10:58.478 8466-8502/? D/OpenGLRenderer: Swap behavior 1
05-18 17:10:58.482 8466-8466/? D/ViewRootImpl@15edd34[MainActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 81 - 0, 0) vi=Rect(0, 81 - 0, 0) or=1
05-18 17:10:58.482 8466-8466/? D/ViewRootImpl@15edd34[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
05-18 17:10:58.482 8466-8466/? I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
05-18 17:10:58.485 8466-8502/? D/libGLESv1: STS_GLApi : DTS is not allowed for Package : zhangphil.app
05-18 17:10:58.488 8466-8479/? D/InputTransport: Input channel constructed: fd=73
05-18 17:11:03.321 8466-8466/zhangphil.app D/1MainActivity: #####开始#####
05-18 17:11:03.321 8466-8466/zhangphil.app D/1数据: 1
05-18 17:11:03.321 8466-8466/zhangphil.app D/1MainActivity: #####结束#####
05-18 17:11:08.321 8466-8466/zhangphil.app D/1MainActivity: #####开始#####
05-18 17:11:08.321 8466-8466/zhangphil.app D/1数据: 2
05-18 17:11:08.322 8466-8466/zhangphil.app D/1MainActivity: #####结束#####
05-18 17:11:13.321 8466-8466/zhangphil.app D/1MainActivity: #####开始#####
05-18 17:11:13.321 8466-8466/zhangphil.app D/1数据: 3
05-18 17:11:13.322 8466-8466/zhangphil.app D/1MainActivity: #####结束#####
05-18 17:11:18.321 8466-8466/zhangphil.app D/1MainActivity: #####开始#####
05-18 17:11:18.321 8466-8466/zhangphil.app D/1数据: 4
05-18 17:11:18.321 8466-8466/zhangphil.app D/1MainActivity: #####结束#####
05-18 17:11:23.321 8466-8466/zhangphil.app D/1MainActivity: #####开始#####
05-18 17:11:23.322 8466-8466/zhangphil.app D/1数据: 5
05-18 17:11:23.322 8466-8466/zhangphil.app D/1MainActivity: #####结束#####
05-18 17:11:28.321 8466-8466/zhangphil.app D/1MainActivity: #####开始#####
05-18 17:11:28.322 8466-8466/zhangphil.app D/1数据: 6
05-18 17:11:28.322 8466-8466/zhangphil.app D/1MainActivity: #####结束#####