2
I would like to perform a task synchronized with the android clock.
For example: in the second 0, then in the second 10, 20, 30, 40 and 50.
I don’t want to run every 10 seconds, I want to run in these exact seconds.
There’s a way to do it?
2
I would like to perform a task synchronized with the android clock.
For example: in the second 0, then in the second 10, 20, 30, 40 and 50.
I don’t want to run every 10 seconds, I want to run in these exact seconds.
There’s a way to do it?
2
To accomplish this you can use the class AlarmManager
and the method setRepeating
, but in the documentation of this method it is said that the use of Handler
s.
In class Handler
you schedule Runnable
s to be executed in the future and also in a given time. The method Handler#postAtTime
gets a Runnable
and a time based on the running time of Android, for example, to schedule every 10 seconds, do:
final AtomicLong tick = new AtomicLong(SystemClock.uptimeMillis()
+ 10000L); // timestamp de 10 segundos no futuro
mHandler = new Handler(); // sem acesso a UI Thread
// mHandler = new Handler(Looper.getMainLooper()); // handler ligado a UI Thread
// ou seja, pode modificar a UI
mHandler.postAtTime(new Runnable() {
@Override
public void run() {
// reagendar-se daqui a 10 segundos
mHandler.postAtTime(this, tick.addAndGet(10000L));
// TODO: realizar tarefa
}
}, tick.get());
All that remains is for you to devise a mechanism to catch the exact time and then go on increasing the tick
.
I hope I’ve helped.
Some useful links:
Browser other questions tagged android
You are not signed in. Login or sign up in order to post.
Rubem thank you very much, I will follow your tips and as soon as I get put here again...
– Luciano Coelho
I did so and it was perfect guy, thanks again: if ( Started == false ) { int seconds = Util.seconds current(); switch(seconds) { case 0: case 10: case 20: case 30: case 40: case 50: Started = true; initialTimerNaHora(); break; } }
– Luciano Coelho
Thanks! If you prefer, accept the answer.
– Ruben O. Chiavone