美文网首页
RxJava倒计时

RxJava倒计时

作者: 小赵不在 | 来源:发表于2020-07-15 19:23 被阅读0次
     private Disposable subscribe;
     private void rxjavaInterval() {
            final Long time = 5L;
            subscribe = Observable.interval(1, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            Log.e("TAG", "倒计时:" + aLong);
                            if (aLong < time && !subscribe.isDisposed()) {
                                tv.setText("记录改变生活" + (time - aLong - 1));
                            } else {
                                Intent intent = new Intent(WelcomActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }
                    });
        }
    
     @Override
        protected void onDestroy() {
            super.onDestroy();
            subscribe.dispose();
            subscribe = null;
        }
    

    相关文章

      网友评论

          本文标题:RxJava倒计时

          本文链接:https://www.haomeiwen.com/subject/ltijhktx.html