CountdownTimer完成时如何通知Observable(How to notify Observable when CountdownTimer is finished)

编程入门 行业动态 更新时间:2024-10-24 08:22:15
CountdownTimer完成时如何通知Observable(How to notify Observable when CountdownTimer is finished)

我有一个自定义的Android TextView,它通过CountDownTimer显示游戏中剩余的时间量

class CountdownTextView(context: Context, attrs: AttributeSet) : TextView(context, attrs) { private lateinit var countDownTimer: CountDownTimer private lateinit var onFinishObservable: Observable<Unit> fun setTime(initTime: Int) { this.text = "$initTime:00" countDownTimer = object : CountDownTimer((initTime *1000).toLong(), 1000) { override fun onTick(millisUntilFinished: Long) { val minutes = millisUntilFinished / 60000 val seconds = (millisUntilFinished % 60000) / 1000 if (seconds / 10 > 0) { text = "$minutes:${(millisUntilFinished % 60000) / 1000}" } else { text = "$minutes:0${(millisUntilFinished % 60000) / 1000}" } } override fun onFinish() { } } fun startCountdown() { countDownTimer.start() } }

如何在countDownTimer的onFinish()方法被调用时设置一个发出值的observable? 我需要这样做,以便在主要活动中,我可以订阅该可观察值并在倒数计时器到期时执行必要的操作。

I have a custom Android TextView which shows the amount of time left in a game via a CountDownTimer

class CountdownTextView(context: Context, attrs: AttributeSet) : TextView(context, attrs) { private lateinit var countDownTimer: CountDownTimer private lateinit var onFinishObservable: Observable<Unit> fun setTime(initTime: Int) { this.text = "$initTime:00" countDownTimer = object : CountDownTimer((initTime *1000).toLong(), 1000) { override fun onTick(millisUntilFinished: Long) { val minutes = millisUntilFinished / 60000 val seconds = (millisUntilFinished % 60000) / 1000 if (seconds / 10 > 0) { text = "$minutes:${(millisUntilFinished % 60000) / 1000}" } else { text = "$minutes:0${(millisUntilFinished % 60000) / 1000}" } } override fun onFinish() { } } fun startCountdown() { countDownTimer.start() } }

How do I set up an observable that emits a value when the countDownTimer's onFinish() method is called? I need this so that on the main activity, I can subscribe to that observable and perform the necessary actions when the countdowntimer expires.

最满意答案

你可以提供一个主题 。

val onFinishObservable = CompletableSubject.create() override fun onFinish() { onFinishObservable.onComplete() }

或者你可以使用Rx作为定时器而不是CountDownTimer 。

fun countDownTimer( time: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS, tick: Long = 1, tickUnit: TimeUnit = TimeUnit.MILLISECONDS ): Observable<Long> { val timeNanos = timeUnit.toNanos(time).also { require(it >= 0) } val tickNanos = tickUnit.toNanos(tick).also { require(it > 0) } val ticks = timeNanos / tickNanos return Observable .intervalRange( 1L, ticks, timeNanos % tickNanos, tickNanos, TimeUnit.NANOSECONDS) .map { ticks - it } .startWith(ticks) } fun start(time: Long, timeUnit: TimeUnit = TimeUnit.SECONDS): Completable { timerSubscription?.dispose() val timer = countDownTimer(time, timeUnit, tickUnit = TimeUnit.SECONDS) timerSubscription = timer.subscribe { text = String.format("%d:%02d", it / 60, it % 60) } return timer.ignoreElements() }

无论哪种方式,调用者都可以订阅该Completable 。

You could provide a Subject.

val onFinishObservable = CompletableSubject.create() override fun onFinish() { onFinishObservable.onComplete() }

Or you could use Rx for the timer instead of CountDownTimer.

fun countDownTimer( time: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS, tick: Long = 1, tickUnit: TimeUnit = TimeUnit.MILLISECONDS ): Observable<Long> { val timeNanos = timeUnit.toNanos(time).also { require(it >= 0) } val tickNanos = tickUnit.toNanos(tick).also { require(it > 0) } val ticks = timeNanos / tickNanos return Observable .intervalRange( 1L, ticks, timeNanos % tickNanos, tickNanos, TimeUnit.NANOSECONDS) .map { ticks - it } .startWith(ticks) } fun start(time: Long, timeUnit: TimeUnit = TimeUnit.SECONDS): Completable { timerSubscription?.dispose() val timer = countDownTimer(time, timeUnit, tickUnit = TimeUnit.SECONDS) timerSubscription = timer.subscribe { text = String.format("%d:%02d", it / 60, it % 60) } return timer.ignoreElements() }

Either way, the caller can subscribe to that Completable.

更多推荐

本文发布于:2023-08-04 11:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   CountdownTimer   Observable   finished   notify

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!