Java / Android Programming - Loop FAIL

后端 未结 3 450
温柔的废话
温柔的废话 2021-01-29 14:39

I am using a while loop with a timer. The thing is that the timer is not used in every loop. It is used only the first time. After the first time the statements included inside

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 15:20

    You're scheduling 10 TimerTasks to execute after an hour, at the same time. So all 10 tasks are being executed after 1 hour, which makes it seem like 1 execute since all the Toast messages display at the same time. To schedule tasks at a fixed delay, with the first task starting in 1 hour, use this method:

    Timer t = new Timer();
    t.schedule(task, 3600000, 3600000);
    

    This will execute until you call t.cancel().

提交回复
热议问题