android RxJava定时器,永远重复,并且可以随时重新启动和停止

bogh5gae  于 2022-12-25  发布在  Android
关注(0)|答案(5)|浏览(424)

在Android中,我使用计时器执行任务,每5秒重复一次,并在1秒后开始,以这种方式:

Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            // Here is the repeated task
        }
    }, /*Start after*/1000, /*Repeats every*/5000);

    // here i stop the timer
    timer.cancel();

这个计时器将重复直到我调用timer.cancel()
我正在使用RxAndroid扩展学习RxJava
所以我发现这个代码在互联网上,我试了一下,它不重复:

Observable.timer(3000, TimeUnit.MILLISECONDS)
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<Long>() {
        @Override
        public void call(Long aLong) {
             // here is the task that should repeat
        }
    });

那么,RxJava中的android定时器的替代方案是什么?

gk7wooem

gk7wooem1#

timer操作符在指定的延迟后发出一个项目,然后完成。我认为你在寻找interval操作符。

Subscription subscription = Observable.interval(1000, 5000, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<Long>() {
                public void call(Long aLong) {
                    // here is the task that should repeat
                }
            });

如果你想停止它,你只是调用取消订阅的订阅:

subscription.unsubscribe()
xnifntxz

xnifntxz2#

调用Observable.repeat()方法重复

Disposable disposable = Observable.timer(3000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.newThread())
.repeat()
.observeOn(AndroidSchedulers.mainThread())
.subscribe();

如果你想阻止它,请调用disposable.dispose()

ctehm74n

ctehm74n3#

Kotlin方式

Observable.timer(5000, TimeUnit.MILLISECONDS)
            .repeat() //to perform your task every 5 seconds
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                Log.d("ComingHere", "Inside_Timer")
            }
ff29svar

ff29svar4#

这是%100正确且安全的工作方式:

//timer variable is in seconds unit
    int timer = 5;
    Disposable disposable = Observable.interval(timer, TimeUnit.SECONDS)
            .map((tick) -> {
                handler.post(() -> {
                    //Enter your CODE here !!!
                });
                return true;
            }).subscribe();

为了阻止它:

if (disposable != null) {
        disposable.dispose();
    }
1aaf6o9v

1aaf6o9v5#

我创建了这个方法,用于将时间从viewModel发送到View:

fun countTime(start: Boolean) {
        var counter = 0
        var seconds: Int
        var minutes: Int
        if (start) {
            disposable = Observable.timer(1, TimeUnit.SECONDS)
                .repeat()
                .subscribe {
                    counter++
                    seconds = (counter % 60)
                    minutes = (counter % 3600) / 60
                    val secondFormatter = String.format("%02d", seconds)
                    val minutesFormatter = String.format("%02d", minutes)
                    timeLiveData.postValue(" $minutesFormatter : $secondFormatter")
                }
        } else {
            if (disposable != null) {
                timeLiveData.postValue("00 : 00")
                disposable.dispose()
            }
        }
    }

相关问题