Reputation: 3807
i want to delay post till the minute change on clock. currently i am using this
handler.postDelayed(drawRunner, 60000);
but this delay post 60 seconds from the time right now. what i want to do is delay the post till minute change on the clock.
for example: lets say time on my phone is 4:15:29
than i want to delay it till the time 4:16:00
and then the next is 4:17:00
and so on.
is there anyway to do this??
Upvotes: 4
Views: 3349
Reputation: 13807
You can access the current time by:
long time = System.currentTimeMillis();
After you have that, you can get the second part of it by:
int seconds = new Date(time).getSeconds();
Then you can substract that from 60, and get the sleep time.
int sleepSecs = 60 - seconds;
Then set that to sleep time to handler.postDelayed()
handler.postDelayed(drawRunner, sleepSecs*1000);
After the first time you use this, you can use a constant 60000 millisecond sleeptime.
Please note, that getSeconds() can return 60 or 61 as second value! Documentation
Upvotes: 5