问题描述
我有一个计时器的服务。我称之为 timer.scheduleAtFixedRate()
在在onStart()
回调方法。但我不得不改变取决于变量的值定时间隔。我发现没有功能的定时器来改变其时间间隔。因此,我认为当条件满足取消计时器并重新安排。但我检查与在TimerTask的本身的条件。所以,当我试图取消计时器在TimerTask的,它抛出 IllegalStateException异常
。任何人都可以给我一些解决方案?
I have a service which has one Timer. I call timer.scheduleAtFixedRate()
in the onStart()
callback method. But I have to alter the timer interval depending on the value of a variable. I found no function for timer to alter its interval. So I thought to cancel the timer and re-schedule when that condition is met. But I am checking the condition with in the timerTask itself. So when I tried to cancel the timer in the TimerTask, it throws IllegalStateException
. Can anybody give me some solution?
推荐答案
Netiher Timer.cancel()
也不 TimerTask.cancel()
抛出 IllegalStateException异常
。当您尝试安排一个任务时引发一个取消定时器。
Netiher Timer.cancel()
nor TimerTask.cancel()
throws IllegalStateException
. That is thrown when you try to schedule a task on a cancelled Timer.
解决方法:不要取消定时
;取消的TimerTask
。
Solution: don't cancel the Timer
; cancel the TimerTask
.
这篇关于如何修改一个java计时器的时间间隔安排它在Android的固定利率后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!