Please tell me the difference between handler.postAtTime and handler.postDelayed in android.And also please guide me when to use handler.postAtTime and when to use handler.postDelayed.
postDelayed(Runnable r, Object token, long delayMillis) Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. final void. removeCallbacks(Runnable r) Remove any pending posts of Runnable r that are in the message queue.
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue.
The postDelayed method takes two parameters Runnable and delayMillis . It adds the Runnable to the thread's message queue to be run after the specified amount of time elapses. The Runnable will execute on the thread to which this handler is attached.
removeCallbacks simply removes those runnables who have not yet begun processing from the queue. Follow this answer to receive notifications.
From documentation:
For postAtTime
:
public final boolean postAtTime (Runnable r, long uptimeMillis)
...
uptimeMillis
The absolute time at which the callback should run, using the uptimeMillis() time-base....
And for postDelayed
:
public final boolean postDelayed (Runnable r, long delayMillis)
...
delayMillis
The delay (in milliseconds) until the Runnable will be executed....
If this is still not clear, postDelayed()
run something after X millisecond from current time. While postAtTime()
runs something at specified time XX:YY:ZZ.mmm
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With