4

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??

Badal
  • 3,738
  • 4
  • 33
  • 60

2 Answers2

5

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

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • 1
    do u know how can i change that postDelayed time to again , like suppose i have set 4000ms sec and now 2000ms pass , now i want to set new interval time like it should be 7000ms , how can i do ? do u know ? – Drim May 06 '16 at 15:01
0

Yes you can use this Timer class to schedule periodic task this way you can change your Clock time in any period of time as you need.

Community
  • 1
  • 1
hackp0int
  • 4,052
  • 8
  • 59
  • 95