RxJava。如何使可观察计时器在后台运行?
我需要确保按下一个按钮后,另一个按钮在15分钟内不可用。为此,我使用如下方法:RxJava。如何使可观察计时器在后台运行?,java,android,timer,background,rx-java,Java,Android,Timer,Background,Rx Java,我需要确保按下一个按钮后,另一个按钮在15分钟内不可用。为此,我使用如下方法: disposableTimer = Observable.timer(15,TimeUnit.MINUTES) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) .subscribe(a
disposableTimer = Observable.timer(15,TimeUnit.MINUTES)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(aLong -> {
buttonSimpleAct.setEnabled(true);
});
当程序处于活动状态时,它工作得很好,但我需要这个计时器在程序关闭时也能工作。
也就是说,如果按下第一个按钮,应用程序关闭并在15分钟后返回,则第二个按钮应该已经处于活动状态
有没有办法让可观察到的在后台工作,或者有其他方法?我要做的是保存(以持久的方式)按钮重新启用的时间戳,并在应用程序返回时用剩余的时间重新启动计时器
// when the first button is clicked:
long reenableAfter = System.currentTimeMillis() + 15 * 60 * 1000L;
save(reenableAfter);
disposableTimer = Observable.timer(15 ,TimeUnit.MINUTES, AndroidSchedulers.mainThread())
.subscribe(aLong -> buttonSimpleAct.setEnabled(true));
//应用程序启动时
long RENEABLEAFTER=load();
disposableTimer=Observable.timer(reenableAfter-System.currentTimeMillis(),
TimeUnit.millides,AndroidSchedulers.mainThread()
.subscribe(沿->按钮simpleact.setEnabled(true));
应用程序在15分钟后关闭并返回,第二个按钮应该已经处于活动状态。
我认为rxjava不会成功,您可能需要某种服务或工作管理您需要在应用程序处于活动状态时添加时间检查,不要添加静态15分钟计时器。哦,我没有想到时间戳。这可能是出路。谢谢你的主意,伙计们