Handler.postAtTime vs Handler.postDelayed

After looking at the source code, the token object eventually passes to the Message:

public final boolean postAtTime(Runnable r, Object token, long uptimeMillis)
308    {
309        return sendMessageAtTime(getPostMessage(r, token), uptimeMillis);
310    }

private static Message getPostMessage(Runnable r, Object token) {
608        Message m = Message.obtain();
609        m.obj = token;

And postDelay

 public final boolean postDelayed(Runnable r, long delayMillis)
330    {
331        return sendMessageDelayed(getPostMessage(r), delayMillis);
332    }

If what you want is

public final boolean postDelayed (Runnable r, Object token, long delay)

Then why not just use

public final boolean postAtTime (Runnable r, Object token, long uptimeMillis)

since its the same.

Update, forgot to add this:

public final boolean sendMessageDelayed(Message msg, long delayMillis)
442    {
443        if (delayMillis < 0) {
444            delayMillis = 0;
445        }
446        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
447    }

Looking at Handler source, it appears that there is :

private final Message getPostMessage(Runnable r, Object token) {
    Message m = Message.obtain();
    m.obj = token;
    m.callback = r;
    return m;
}

Which can be copied for what you want : Instead of calling postDelayed, wrap your runnable in such a message

sendMessageDelayed(getPostMessage(r, token), delayMillis);

you can then use removeCallbacks() with token as param

Tags:

Java

Android