Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Difference between handler.postAtTime and handler.postDelayed

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.

like image 450
Vijay Avatar asked Sep 29 '11 06:09

Vijay


People also ask

What is Handler postDelayed in Android?

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.

What are handlers in Android?

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.

How do you use kotlin handler postDelayed?

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.

What is Handler removeCallbacks?

removeCallbacks simply removes those runnables who have not yet begun processing from the queue. Follow this answer to receive notifications.


1 Answers

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.

like image 165
inazaruk Avatar answered Nov 04 '22 17:11

inazaruk