本文介绍了如何改变一个java计时器的时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个机器人的俄罗斯方块游戏。和执行时出现一个IllegalStateException

  timer.scheduleAtFixedRate(任务,0L,毫秒);

 公共无效setTimerInterval(INT毫秒){
    timer.cancel();
    定时器=新定时器();
    timer.scheduleAtFixedRate(任务,0L,毫秒);
}

我是不是错什么这样做呢?
我需要取消计时器,并创建一个新的,因为我无法改变定时器的时间间隔,除非你安排一个新的任务,对吧?

我看了一个帖子here这里是其中一个答案的话:

I didn't use the accepted answer of the that question because it's about pausing and resuming the timer.

I reinstantiated the timer as shown above but there is still a IllegalStateException.

解决方案
Handler handler = new Handler();
handler.post(new Runnable() {

    @Override
    public void run() {
        // 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.

这篇关于如何改变一个java计时器的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:57
查看更多