Skip to content Skip to sidebar Skip to footer

How To Change A Java Timer's Interval

I'm working on an android Tetris game. And an IllegalStateException occurred when executing timer.scheduleAtFixedRate (task, 0L, milliseconds); in public void setTimerInterval (in

Solution 1:

Handlerhandler=newHandler();
handler.post(newRunnable() {

    @Overridepublicvoidrun() {
        // do the task here
        handler.postDelayed(this, milliseconds); // set time here to refresh textView
    }
});

Make the milliseconds global and change that, maybe that would be a better solution.

Solution 2:

How to change a java timer's interval

Java Timers don't have intervals. Timer tasks have intervals.

Answer : cancel and reschedule the TimerTask, not the Timer.

Post a Comment for "How To Change A Java Timer's Interval"