我想推迟发布,直到时钟的分钟变化。目前我正在使用这个
handler.postDelayed(drawRunner, 60000);
但是这个延迟是从现在开始的 60 秒。我想要做的是将帖子延迟到时钟上的分钟变化。
例如:假设我手机上的时间4:15:29
比我想延迟到时间4:16:00
然后下一个是4:17:00
等等。
有没有办法做到这一点?
您可以通过以下方式访问当前时间:
long time = System.currentTimeMillis();
之后,您可以通过以下方式获得它的第二部分:
int seconds = new Date(time).getSeconds();
然后你可以从 60 中减去它,得到睡眠时间。
int sleepSecs = 60 - seconds;
然后将其设置为睡眠时间handler.postDelayed()
handler.postDelayed(drawRunner, sleepSecs*1000);
第一次使用后,您可以使用恒定的 60000 毫秒睡眠时间。
请注意,getSeconds() 可以返回 60 或 61 作为第二个值!文档
是的,您可以使用此 Timer 类来安排定期任务,这样您就可以根据需要在任何时间段内更改时钟时间。