How to change java timer interval after scheduling it for fixed rate in android?

I have a service that has one timer. I am timer.scheduleAtFixedRate()

calling in a callback method onStart()

. But I need to change the timer interval depending on the value of the variable. I have not found a function for the timer to change its interval. So I decided to cancel the timer and reschedule when this condition is met. But I am checking the condition with the timer itself. So when I tried to cancel the timer in the TimerTask it throws IllegalStateException

. Can anyone give me some solution?

+2


source to share


2 answers


Netiher Timer.cancel()

and TimerTask.cancel()

throws IllegalStateException

. It throws itself when you try to schedule a task on canceledTimer.



Solution: do not cancel Timer

; cancel TimerTask

.

0


source


The timer can only be assigned once. If an IllegalStateException does not occur when you call cancel (), but when you try to reschedule the timer, just restore the timer and then schedule it. If not, I'm not sure.



-1


source







All Articles