HandlerCompat

Added in 1.1.0

public final class HandlerCompat


Helper for accessing features in Handler.

Summary

Public methods

static @NonNull Handler

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

static @NonNull Handler

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

static boolean

Checks if there are any pending posts of messages with callback r in the message queue.

static boolean
postDelayed(
    @NonNull Handler handler,
    @NonNull Runnable r,
    @Nullable 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.

Public methods

createAsync

Added in 1.1.0
public static @NonNull Handler createAsync(@NonNull Looper looper)

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

Messages sent to an async handler are guaranteed to be ordered with respect to one another, but not necessarily with respect to messages from other Handlers.

Parameters
@NonNull Looper looper

the Looper that the new Handler should be bound to

Returns
@NonNull Handler

a new async Handler instance

See also
createAsync

to create an async Handler with custom message handling. Compatibility behavior:

  • SDK 28 and above, this method matches platform behavior.
  • SDK 27 and earlier, this method attempts to call the platform API via reflection, but may fail and return a synchronous handler instance.
createAsync

createAsync

Added in 1.1.0
public static @NonNull Handler createAsync(@NonNull Looper looper, @NonNull Handler.Callback callback)

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

Messages sent to an async handler are guaranteed to be ordered with respect to one another, but not necessarily with respect to messages from other Handlers.

Parameters
@NonNull Looper looper

the Looper that the new Handler should be bound to

@NonNull Handler.Callback callback

callback to send events to

Returns
@NonNull Handler

a new async Handler instance

See also
createAsync

to create an async Handler without custom message handling. Compatibility behavior:

  • SDK 28 and above, this method matches platform behavior.
  • SDK 27 and earlier, this method attempts to call the platform API via reflection, but may fail and return a synchronous handler instance.
createAsync

hasCallbacks

Added in 1.6.0
public static boolean hasCallbacks(@NonNull Handler handler, @NonNull Runnable r)

Checks if there are any pending posts of messages with callback r in the message queue. Compatibility behavior:

  • SDK 29 and above, this method matches platform behavior.
  • SDK 28 and earlier, this method attempts to call the platform API via reflection, but will throw an unchecked exception if the method has been altered from the AOSP implementation and cannot be called. This is unlikely, but there is no safe fallback case for this method and we must throw an exception as a result.
Parameters
@NonNull Handler handler

handler on which to call the method

@NonNull Runnable r

callback to look for in the message queue

Returns
boolean

true if the callback is in the message queue

See also
hasCallbacks

postDelayed

Added in 1.1.0
public static boolean postDelayed(
    @NonNull Handler handler,
    @NonNull Runnable r,
    @Nullable 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. The runnable will be run on the thread to which this handler is attached. The time-base is uptimeMillis. Time spent in deep sleep will add an additional delay to execution.

Parameters
@NonNull Handler handler

handler to use for posting the runnable.

@NonNull Runnable r

The Runnable that will be executed.

@Nullable Object token

An instance which can be used to cancel r via removeCallbacksAndMessages.

long delayMillis

The delay (in milliseconds) until the Runnable will be executed.

Returns
boolean

Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.

See also
postDelayed