您现在的位置是:首页 > 文章详情

RxJava2 / RxAndroid2操作符interval:每隔若干时间发射信号

日期:2017-05-17点击:443
RxJava2 / RxAndroid2操作符interval:每隔若干时间发射信号


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: #####结束#####


原文链接:https://yq.aliyun.com/articles/615864
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章