# ContextKt

Added in 1.1.0

*** ** * ** ***


```
public final class ContextKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/core/content/ContextKt#(android.content.Context).getSystemService()(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html receiver)` Return the handle to a system-level service by class. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Void.html` | `https://developer.android.com/reference/androidx/core/content/ContextKt#(android.content.Context).receiveBroadcasts(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.Function2)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/IntentFilter.html filter, int flags, https://developer.android.com/reference/java/lang/String.html broadcastPermission, https://developer.android.com/reference/android/os/Handler.html scheduler, @https://developer.android.com/reference/androidx/annotation/NonNull Function2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/BroadcastReceiver.html, https://developer.android.com/reference/android/content/Intent.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onReceive )` Registers a `https://developer.android.com/reference/android/content/BroadcastReceiver.html` until the coroutine is cancelled. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Void.html` | `https://developer.android.com/reference/androidx/core/content/ContextKt#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/IntentFilter.html filter, int flags, https://developer.android.com/reference/java/lang/String.html broadcastPermission, https://developer.android.com/reference/android/os/Handler.html scheduler, @https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html, https://developer.android.com/reference/android/content/Intent.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onReceive )` Register a `https://developer.android.com/reference/android/content/BroadcastReceiver.html` until the coroutine is cancelled. |
| `static final void` | `https://developer.android.com/reference/androidx/core/content/ContextKt#(android.content.Context).withStyledAttributes(kotlin.Int,kotlin.IntArray,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html receiver, @https://developer.android.com/reference/androidx/annotation/StyleRes int resourceId, @https://developer.android.com/reference/androidx/annotation/NonNull int[] attrs, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/res/TypedArray.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` Executes `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(kotlin.Int,kotlin.IntArray,kotlin.Function1)` on a `https://developer.android.com/reference/android/content/res/TypedArray.html` receiver. |
| `static final void` | `https://developer.android.com/reference/androidx/core/content/ContextKt#(android.content.Context).withStyledAttributes(android.util.AttributeSet,kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html receiver, https://developer.android.com/reference/android/util/AttributeSet.html set, @https://developer.android.com/reference/androidx/annotation/NonNull int[] attrs, @https://developer.android.com/reference/androidx/annotation/AttrRes int defStyleAttr, @https://developer.android.com/reference/androidx/annotation/StyleRes int defStyleRes, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/res/TypedArray.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` Executes `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(android.util.AttributeSet,kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Function1)` on a `https://developer.android.com/reference/android/content/res/TypedArray.html` receiver. |

## Public methods

### getSystemService

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/content/Context.kt+function:getSystemService)

```
public static final T <T extends Object> getSystemService(@NonNull Context receiver)
```

Return the handle to a system-level service by class.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/content/ContextCompat#getSystemService(android.content.Context,java.lang.Class<T>)` |   |

### receiveBroadcasts

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/content/Context.kt+function:receiveBroadcasts)

```
public static final @NonNull Void receiveBroadcasts(
    @NonNull Context receiver,
    @NonNull IntentFilter filter,
    int flags,
    String broadcastPermission,
    Handler scheduler,
    @NonNull Function2<@NonNull BroadcastReceiver, Intent, Unit> onReceive
)
```

Registers a `https://developer.android.com/reference/android/content/BroadcastReceiver.html` until the coroutine is cancelled.

Equivalent to calling `https://developer.android.com/reference/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`, and when the coroutine is cancelled calling `https://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)`, but exceptions from `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcasts(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.Function2)` will propagate to the caller (unregistering the receiver in the process).

The rules of `https://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)` apply here. You can use the receiver `https://developer.android.com/reference/android/content/BroadcastReceiver.html` to view/modify the current result values.

`https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcasts(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.Function2)` runs on the provided `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcasts(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.Function2)` thread, or the main thread if it's null. This means it can continue running even after `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcasts(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.Function2)` is cancelled. To propagate cancellation, consider using `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)`.

If you wish to process broadcasts asynchronously (and concurrently), consider using `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)`, or manually use `https://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync()`.

Example usage:

```
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.content.IntentFilter
import androidx.core.content.ContextCompat
import androidx.core.content.receiveBroadcasts
import kotlinx.coroutines.coroutineScope

coroutineScope {
  val job = launch {
    context.receiveBroadcasts(
      filter = IntentFilter(Intent.ACTION_TIME_CHANGED),
      flags = ContextCompat.RECEIVER_EXPORTED,
    ) { intent: Intent? ->
      // Process intent
      setResultCode(RESULT_OK) // Set broadcast result
    }
  }
  ...
  job.cancel() // Unregister
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/IntentFilter.html filter` | Selects the Intent broadcasts to be received. |
| `int flags` | If this receiver is listening for broadcasts sent from other apps - even other apps that you own - use the `https://developer.android.com/reference/androidx/core/content/ContextCompat#RECEIVER_EXPORTED()` flag. If instead this receiver is listening only for broadcasts sent by your app, or from the system UID, use the `https://developer.android.com/reference/androidx/core/content/ContextCompat#RECEIVER_NOT_EXPORTED()` flag. |
| `https://developer.android.com/reference/java/lang/String.html broadcastPermission` | String naming a permission that a broadcaster must hold in order to send and Intent to you. If null, no permission is required. |
| `https://developer.android.com/reference/android/os/Handler.html scheduler` | Handler identifying the thread will receive the Intent. If null, the main thread of the process will be used. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/BroadcastReceiver.html, https://developer.android.com/reference/android/content/Intent.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onReceive` | The callback equivalent of `https://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)`, taking the `https://developer.android.com/reference/android/content/BroadcastReceiver.html` as a receiver. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)` |   |
| `https://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)` |   |

### receiveBroadcastsAsync

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/content/Context.kt+function:receiveBroadcastsAsync)

```
public static final @NonNull Void receiveBroadcastsAsync(
    @NonNull Context receiver,
    @NonNull IntentFilter filter,
    int flags,
    String broadcastPermission,
    Handler scheduler,
    @NonNull SuspendFunction2<@NonNull BroadcastReceiver.PendingResult, Intent, Unit> onReceive
)
```

Register a `https://developer.android.com/reference/android/content/BroadcastReceiver.html` until the coroutine is cancelled.

Equivalent to calling `https://developer.android.com/reference/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`, and when the coroutine is cancelled calling `https://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)`, but uses `https://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync()` to allow suspending `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)` and calls `https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html#finish()` when `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)` returns or throws.

The rules of `https://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync()` apply here. You can use the receiver `https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html` to view/modify the current result values. If you wish to use the `https://developer.android.com/reference/android/content/BroadcastReceiver.html`, consider using `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcasts(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.Function2)`.

Each `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)` is invoked concurrently in a child coroutine of the current context, and is cancelled when `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)` is cancelled. If any `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)` throws, all other concurrent invocations are cancelled and the exception is propagated to the caller (unregistering the receiver in the process).

**Do not call `https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html#finish()` yourself, this is done for you when `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)` returns or throws.**

Example usage:

```
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.content.IntentFilter
import androidx.core.content.ContextCompat
import androidx.core.content.receiveBroadcasts
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay

coroutineScope {
  val job = launch {
    context.receiveBroadcastsAsync(
      filter = IntentFilter(Intent.ACTION_TIME_CHANGED),
      flags = ContextCompat.RECEIVER_EXPORTED,
    ) { intent: Intent? ->
      delay(100) // Process intent with suspending work
      setResultCode(RESULT_OK) // Set broadcast result
    }
  }
  ...
  job.cancel() // Unregister and cancel ongoing callbacks.
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/IntentFilter.html filter` | Selects the Intent broadcasts to be received. |
| `int flags` | If this receiver is listening for broadcasts sent from other apps - even other apps that you own - use the `https://developer.android.com/reference/androidx/core/content/ContextCompat#RECEIVER_EXPORTED()` flag. If instead this receiver is listening only for broadcasts sent by your app, or from the system UID, use the `https://developer.android.com/reference/androidx/core/content/ContextCompat#RECEIVER_NOT_EXPORTED()` flag. |
| `https://developer.android.com/reference/java/lang/String.html broadcastPermission` | String naming a permission that a broadcaster must hold in order to send and Intent to you. If null, no permission is required. |
| `https://developer.android.com/reference/android/os/Handler.html scheduler` | Handler identifying the thread will receive the Intent. If null, the main thread of the process will be used. Note that this is not used to run `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)`, but if the handler is blocked `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).receiveBroadcastsAsync(android.content.IntentFilter,kotlin.Int,kotlin.String,android.os.Handler,kotlin.coroutines.SuspendFunction2)` will not execute. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html, https://developer.android.com/reference/android/content/Intent.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onReceive` | The callback equivalent of `https://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)`, taking the `https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html` as receiver. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)` |   |
| `https://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)` |   |

### withStyledAttributes

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/content/Context.kt+function:withStyledAttributes) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final void withStyledAttributes(
    @NonNull Context receiver,
    @StyleRes int resourceId,
    @NonNull int[] attrs,
    @NonNull Function1<@NonNull TypedArray, Unit> block
)
```

Executes `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(kotlin.Int,kotlin.IntArray,kotlin.Function1)` on a `https://developer.android.com/reference/android/content/res/TypedArray.html` receiver. The `https://developer.android.com/reference/android/content/res/TypedArray.html` holds the values defined by the style resource `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(kotlin.Int,kotlin.IntArray,kotlin.Function1)` which are listed in `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(kotlin.Int,kotlin.IntArray,kotlin.Function1)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/StyleRes int resourceId` | The desired style resource. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull int[] attrs` | The desired attributes. These attribute IDs must be sorted in ascending order. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/res/TypedArray.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block` | The block that will be executed. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#obtainStyledAttributes(android.util.AttributeSet, int[])` |   |
| `https://developer.android.com/reference/android/content/res/Resources.Theme.html#obtainStyledAttributes(android.util.AttributeSet, int[], int, int)` |   |

### withStyledAttributes

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/content/Context.kt+function:withStyledAttributes) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final void withStyledAttributes(
    @NonNull Context receiver,
    AttributeSet set,
    @NonNull int[] attrs,
    @AttrRes int defStyleAttr,
    @StyleRes int defStyleRes,
    @NonNull Function1<@NonNull TypedArray, Unit> block
)
```

Executes `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(android.util.AttributeSet,kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Function1)` on a `https://developer.android.com/reference/android/content/res/TypedArray.html` receiver. The `https://developer.android.com/reference/android/content/res/TypedArray.html` holds the attribute values in `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(android.util.AttributeSet,kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Function1)` that are listed in `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(android.util.AttributeSet,kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Function1)`. In addition, if the given `https://developer.android.com/reference/android/util/AttributeSet.html` specifies a style class (through the `style` attribute), that style will be applied on top of the base attributes it defines.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/util/AttributeSet.html set` | The base set of attribute values. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull int[] attrs` | The desired attributes to be retrieved. These attribute IDs must be sorted in ascending order. |
| `@https://developer.android.com/reference/androidx/annotation/AttrRes int defStyleAttr` | An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the `https://developer.android.com/reference/android/content/res/TypedArray.html`. Can be 0 to not look for defaults. |
| `@https://developer.android.com/reference/androidx/annotation/StyleRes int defStyleRes` | A resource identifier of a style resource that supplies default values for the `https://developer.android.com/reference/android/content/res/TypedArray.html`, used only if `https://developer.android.com/reference/androidx/core/content/package-summary#(android.content.Context).withStyledAttributes(android.util.AttributeSet,kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Function1)` is 0 or can not be found in the theme. Can be 0 to not look for defaults. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/res/TypedArray.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block` | The block that will be executed. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#obtainStyledAttributes(android.util.AttributeSet, int[])` |   |
| `https://developer.android.com/reference/android/content/res/Resources.Theme.html#obtainStyledAttributes(android.util.AttributeSet, int[], int, int)` |   |