一, 首先引入RxJava依赖
implementation'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation'io.reactivex.rxjava3:rxjava:3.0.0'
引入RxJava依赖,倒计时方法二, 实现倒计时方法
private void timeDown() {
subscribe = Observable.interval(0L, 1L, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(Long aLong)throws Throwable {
Long offset =9 - aLong;
//取消
if (offset <=0) {
if (subscribe !=null && !subscribe.isDisposed())
subscribe.dispose();
bt_skip.setEnabled(true);
goLoginPage();
}else
bt_skip.setText(offset +" 取消");
}
});
}
倒计时方法三, 在onDestroy()方法里面解绑Disposable
@Override
protected void onDestroy() {
/**
* 一定要解绑Disposable否则内存溢出
* */
if (!subscribe.isDisposed()) {
subscribe.dispose();
}
super.onDestroy();
}
四, 调用倒计时方法
调用倒计时方法End.
网友评论