# HandlerCompat

# HandlerCompat

Artifact:[androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/os/HandlerCompat.java+class:androidx.core.os.HandlerCompat)  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/os/HandlerCompat "View this page in Java")

<br />

```
class HandlerCompat
```

<br />

*** ** * ** ***

Helper for accessing features in[Handler](https://developer.android.com/reference/android/os/Handler.html).

## Summary

|                                       ### Public functions                                        |
|---------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[Handler](https://developer.android.com/reference/android/os/Handler.html)          | [createAsync](https://developer.android.com/reference/kotlin/androidx/core/os/HandlerCompat#createAsync(android.os.Looper))`(looper: `[Looper](https://developer.android.com/reference/android/os/Looper.html)`)` Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.                                                                                                                                                                                                                                                                                                       |
| `java-static `[Handler](https://developer.android.com/reference/android/os/Handler.html)          | [createAsync](https://developer.android.com/reference/kotlin/androidx/core/os/HandlerCompat#createAsync(android.os.Looper,android.os.Handler.Callback))`(looper: `[Looper](https://developer.android.com/reference/android/os/Looper.html)`, callback: `[Handler.Callback](https://developer.android.com/reference/android/os/Handler.Callback.html)`)` Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.                                                                                                                                                                 |
| `java-static `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [hasCallbacks](https://developer.android.com/reference/kotlin/androidx/core/os/HandlerCompat#hasCallbacks(android.os.Handler,java.lang.Runnable))`(handler: `[Handler](https://developer.android.com/reference/android/os/Handler.html)`, r: `[Runnable](https://developer.android.com/reference/java/lang/Runnable.html)`)` Checks if there are any pending posts of messages with callback`r`in the message queue.                                                                                                                                                                                                                                |
| `java-static `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [postDelayed](https://developer.android.com/reference/kotlin/androidx/core/os/HandlerCompat#postDelayed(android.os.Handler,java.lang.Runnable,java.lang.Object,long))`(handler: `[Handler](https://developer.android.com/reference/android/os/Handler.html)`, r: `[Runnable](https://developer.android.com/reference/java/lang/Runnable.html)`, token: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?, delayMillis: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. |

## Public functions

### createAsync

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun createAsync(looper: Looper): Handler
```

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                                     |
|------------------------------------------------------------------------------------|----------------------------------------------------|
| `looper: `[Looper](https://developer.android.com/reference/android/os/Looper.html) | the Looper that the new Handler should be bound to |

|                                  Returns                                   |
|----------------------------------------------------------------------------|------------------------------|
| [Handler](https://developer.android.com/reference/android/os/Handler.html) | a new async Handler instance |

|                                                                  See also                                                                  |
|--------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [createAsync](https://developer.android.com/reference/android/os/Handler.html#createAsync(android.os.Looper, android.os.Handler.Callback)) | 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](https://developer.android.com/reference/android/os/Handler.html#createAsync(android.os.Looper))                              |                                                                                                                                                                                                                                                                                         |

### createAsync

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun createAsync(looper: Looper, callback: Handler.Callback): Handler
```

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                                                |
|----------------------------------------------------------------------------------------------------------|----------------------------------------------------|
| `looper: `[Looper](https://developer.android.com/reference/android/os/Looper.html)                       | the Looper that the new Handler should be bound to |
| `callback: `[Handler.Callback](https://developer.android.com/reference/android/os/Handler.Callback.html) | callback to send events to                         |

|                                  Returns                                   |
|----------------------------------------------------------------------------|------------------------------|
| [Handler](https://developer.android.com/reference/android/os/Handler.html) | a new async Handler instance |

|                                                                  See also                                                                  |
|--------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [createAsync](https://developer.android.com/reference/kotlin/androidx/core/os/HandlerCompat#createAsync(android.os.Looper))                | 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](https://developer.android.com/reference/android/os/Handler.html#createAsync(android.os.Looper, android.os.Handler.Callback)) |                                                                                                                                                                                                                                                                                            |

### hasCallbacks

Added in[1.6.0](https://developer.android.com/jetpack/androidx/releases/core#1.6.0)  

```
java-static fun hasCallbacks(handler: Handler, r: Runnable): Boolean
```

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                                       |
|---------------------------------------------------------------------------------------|-------------------------------------------|
| `handler: `[Handler](https://developer.android.com/reference/android/os/Handler.html) | handler on which to call the method       |
| `r: `[Runnable](https://developer.android.com/reference/java/lang/Runnable.html)      | callback to look for in the message queue |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|-----------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | `true`if the callback is in the message queue |

|                                                     See also                                                     |
|------------------------------------------------------------------------------------------------------------------|---|
| [hasCallbacks](https://developer.android.com/reference/android/os/Handler.html#hasCallbacks(java.lang.Runnable)) |   |

### postDelayed

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun postDelayed(handler: Handler, r: Runnable, token: Any?, delayMillis: Long): Boolean
```

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](https://developer.android.com/reference/android/os/SystemClock.html#uptimeMillis()).**Time spent in deep sleep will add an additional delay to execution.  

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `handler: `[Handler](https://developer.android.com/reference/android/os/Handler.html)        | handler to use for posting the runnable.                                                                                                                                                 |
| `r: `[Runnable](https://developer.android.com/reference/java/lang/Runnable.html)             | The Runnable that will be executed.                                                                                                                                                      |
| `token: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?`      | An instance which can be used to cancel`r`via[removeCallbacksAndMessages](https://developer.android.com/reference/android/os/Handler.html#removeCallbacksAndMessages(java.lang.Object)). |
| `delayMillis: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The delay (in milliseconds) until the Runnable will be executed.                                                                                                                         |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | 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](https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, java.lang.Object, long)) |   |