How to change java timer interval
I am working on Android Tetris. And an IllegalStateException occurred while executing
timer.scheduleAtFixedRate (task, 0L, milliseconds);
in
public void setTimerInterval (int milliseconds) {
timer.cancel ();
timer = new Timer ();
timer.scheduleAtFixedRate (task, 0L, milliseconds);
}
Am I doing it wrong or something? I need to cancel the timer and create a new one because I can't change the timer interval unless you schedule a new task for it, right?
I read the post here and here is a quote from one of the replies:
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, simply restore the timer and then schedule it. Otherwise, I'm not sure.
I didn't use the accepted answer of this question because it was about pausing and resuming a timer.
I rebuilt the timer as shown above, but there is still an IllegalStateException.
0
source to share
2 answers