# ContextCompat

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

*** ** * ** ***


```
class ContextCompat
```

<br />

Known direct subclasses [ActivityCompat](https://developer.android.com/reference/kotlin/androidx/core/app/ActivityCompat)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/app/ActivityCompat` | Helper for accessing features in `https://developer.android.com/reference/android/app/Activity.html`. |

*** ** * ** ***

Helper for accessing features in `https://developer.android.com/reference/android/content/Context.html`.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#RECEIVER_EXPORTED() = 2` Flag for `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`: The receiver can receive broadcasts from other Apps. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#RECEIVER_NOT_EXPORTED() = 4` Flag for `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`: The receiver cannot receive broadcasts from other Apps. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#RECEIVER_VISIBLE_TO_INSTANT_APPS() = 1` Flag for `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`: The receiver can receive broadcasts from Instant Apps. |

| ### Protected constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#ContextCompat()()` This class should not be instantiated, but the constructor must be visible for the class to be extended (ex. in ActivityCompat). |

| ### Public functions |
|---|---|
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#checkSelfPermission(android.content.Context,java.lang.String)(context: https://developer.android.com/reference/android/content/Context.html, permission: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Determine whether *you* have been granted a particular permission. |
| `java-static https://developer.android.com/reference/android/content/Context.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#createAttributionContext(android.content.Context,java.lang.String)(context: https://developer.android.com/reference/android/content/Context.html, attributionTag: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?)` Return a new Context object for the current Context but attribute to a different tag. |
| `java-static https://developer.android.com/reference/android/content/Context.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#createDeviceProtectedStorageContext(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Return a new Context object for the current Context but whose storage APIs are backed by device-protected storage. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getAttributionTag(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Attribution can be used in complex apps to logically separate parts of the app. |
| `java-static https://developer.android.com/reference/java/io/File.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getCodeCacheDir(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Returns the absolute path to the application specific cache directory on the filesystem designed for storing cached code. |
| `java-static @https://developer.android.com/reference/kotlin/androidx/annotation/ColorInt https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getColor(android.content.Context,int)(context: https://developer.android.com/reference/android/content/Context.html, id: @https://developer.android.com/reference/kotlin/androidx/annotation/ColorRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns a color associated with a particular resource ID |
| `java-static https://developer.android.com/reference/android/content/res/ColorStateList.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getColorStateList(android.content.Context,int)(context: https://developer.android.com/reference/android/content/Context.html, id: @https://developer.android.com/reference/kotlin/androidx/annotation/ColorRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns a color state list associated with a particular resource ID. |
| `java-static https://developer.android.com/reference/android/content/Context.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getContextForLanguage(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Gets the context which respects the per-app locales locale. |
| `java-static https://developer.android.com/reference/java/io/File.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getDataDir(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Returns the absolute path to the directory on the filesystem where all private files belonging to this app are stored. |
| `java-static https://developer.android.com/reference/android/view/Display.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getDisplayOrDefault(android.content.Context)(context: @https://developer.android.com/reference/kotlin/androidx/annotation/DisplayContext https://developer.android.com/reference/android/content/Context.html)` Get the display this context is associated with or the `https://developer.android.com/reference/android/view/Display.html#DEFAULT_DISPLAY` as the fallback if the context is not associated with any `https://developer.android.com/reference/android/view/Display.html`. |
| `java-static https://developer.android.com/reference/android/graphics/drawable/Drawable.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getDrawable(android.content.Context,int)(context: https://developer.android.com/reference/android/content/Context.html, id: @https://developer.android.com/reference/kotlin/androidx/annotation/DrawableRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns a drawable object associated with a particular resource ID. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/java/io/File.html!>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/ReplaceWith(expression = "context.getExternalCacheDirs()") [getExternalCacheDirs](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getExternalCacheDirs(android.content.Context))(context: https://developer.android.com/reference/android/content/Context.html)` **This function is deprecated.** Call `https://developer.android.com/reference/android/content/Context.html#getExternalCacheDirs()` directly. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/java/io/File.html!>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/ReplaceWith(expression = "context.getExternalFilesDirs(type)") [getExternalFilesDirs](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getExternalFilesDirs(android.content.Context,java.lang.String))(context: https://developer.android.com/reference/android/content/Context.html, type: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?)` **This function is deprecated.** Call getExternalFilesDirs directly. <br /> |
| `java-static https://developer.android.com/reference/java/util/concurrent/Executor.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getMainExecutor(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Return an `https://developer.android.com/reference/java/util/concurrent/Executor.html` that will run enqueued tasks on the main thread associated with this context. |
| `java-static https://developer.android.com/reference/java/io/File.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getNoBackupFilesDir(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Returns the absolute path to the directory on the filesystem similar to `https://developer.android.com/reference/android/content/Context.html#getFilesDir()`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/java/io/File.html!>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/ReplaceWith(expression = "context.getObbDirs()") [getObbDirs](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getObbDirs(android.content.Context))(context: https://developer.android.com/reference/android/content/Context.html)` **This function is deprecated.** Call `https://developer.android.com/reference/android/content/Context.html#getObbDirs()` directly. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getString(android.content.Context,int)(context: https://developer.android.com/reference/android/content/Context.html, resId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Gets the resource string that also respects the per-app locales. |
| `java-static T?` | `<T> https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getSystemService(android.content.Context,java.lang.Class<T>)(context: https://developer.android.com/reference/android/content/Context.html, serviceClass: https://developer.android.com/reference/java/lang/Class.html<T!>)` Return the handle to a system-level service by class. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getSystemServiceName(android.content.Context,java.lang.Class<?>)(context: https://developer.android.com/reference/android/content/Context.html, serviceClass: https://developer.android.com/reference/java/lang/Class.html<*>)` Gets the name of the system-level service that is represented by the specified class. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#isDeviceProtectedStorage(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Indicates if the storage APIs of this Context are backed by device-encrypted storage. |
| `java-static https://developer.android.com/reference/android/content/Intent.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)( context: https://developer.android.com/reference/android/content/Context.html, receiver: https://developer.android.com/reference/android/content/BroadcastReceiver.html?, filter: https://developer.android.com/reference/android/content/IntentFilter.html, flags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Register a broadcast receiver. |
| `java-static https://developer.android.com/reference/android/content/Intent.html?` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,java.lang.String,android.os.Handler,int)( context: https://developer.android.com/reference/android/content/Context.html, receiver: https://developer.android.com/reference/android/content/BroadcastReceiver.html?, filter: https://developer.android.com/reference/android/content/IntentFilter.html, broadcastPermission: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, scheduler: https://developer.android.com/reference/android/os/Handler.html?, flags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Register a broadcast receiver. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#startActivities(android.content.Context,android.content.Intent[])(context: https://developer.android.com/reference/android/content/Context.html, intents: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/android/content/Intent.html!>)` Start a set of activities as a synthesized task stack, if able. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#startActivities(android.content.Context,android.content.Intent[],android.os.Bundle)(context: https://developer.android.com/reference/android/content/Context.html, intents: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/android/content/Intent.html!>, options: https://developer.android.com/reference/android/os/Bundle.html?)` Start a set of activities as a synthesized task stack, if able. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/ReplaceWith(expression = "context.startActivity(intent, options)") [startActivity](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#startActivity(android.content.Context,android.content.Intent,android.os.Bundle))(context: https://developer.android.com/reference/android/content/Context.html, intent: https://developer.android.com/reference/android/content/Intent.html, options: https://developer.android.com/reference/android/os/Bundle.html?)` **This function is deprecated.** Call startActivity directly. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#startForegroundService(android.content.Context,android.content.Intent)(context: https://developer.android.com/reference/android/content/Context.html, intent: https://developer.android.com/reference/android/content/Intent.html)` startForegroundService() was introduced in O, just call startService for before O. |

## Constants

### RECEIVER_EXPORTED

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

```
const val RECEIVER_EXPORTED = 2: Int
```

Flag for `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`: The receiver can receive broadcasts from other Apps. Has the same behavior as marking a statically registered receiver with "exported=true"

### RECEIVER_NOT_EXPORTED

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

```
const val RECEIVER_NOT_EXPORTED = 4: Int
```

Flag for `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`: The receiver cannot receive broadcasts from other Apps. Has the same behavior as marking a statically registered receiver with "exported=false"

### RECEIVER_VISIBLE_TO_INSTANT_APPS

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

```
const val RECEIVER_VISIBLE_TO_INSTANT_APPS = 1: Int
```

Flag for `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int)`: The receiver can receive broadcasts from Instant Apps.

## Protected constructors

### ContextCompat

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

```
protected ContextCompat()
```

This class should not be instantiated, but the constructor must be visible for the class to be extended (ex. in ActivityCompat).

## Public functions

### checkSelfPermission

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

```
java-static fun checkSelfPermission(context: Context, permission: String): Int
```

Determine whether *you* have been granted a particular permission.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | context for which to check the permission. |
| `permission: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The name of the permission being checked. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/android/content/pm/PackageManager.html#PERMISSION_GRANTED` if you have the permission, or `https://developer.android.com/reference/android/content/pm/PackageManager.html#PERMISSION_DENIED` if not. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/pm/PackageManager.html#checkPermission(java.lang.String, java.lang.String)` |   |

### createAttributionContext

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

```
java-static fun createAttributionContext(context: Context, attributionTag: String?): Context
```

Return a new Context object for the current Context but attribute to a different tag. In complex apps attribution tagging can be used to distinguish between separate logical parts.

Compatibility behavior:

- API 30 and above, returns a new Context object with the specified attribution tag
- API 29 and earlier, returns the original `context` with no attribution tag

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The current context. |
| `attributionTag: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The tag or `null` to create a context for the default. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html` | A `https://developer.android.com/reference/android/content/Context.html` that is tagged for the new attribution |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getAttributionTag(android.content.Context)` |   |

### createDeviceProtectedStorageContext

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

```
java-static fun createDeviceProtectedStorageContext(context: Context): Context?
```

Return a new Context object for the current Context but whose storage APIs are backed by device-protected storage.

On devices with direct boot, data stored in this location is encrypted with a key tied to the physical device, and it can be accessed immediately after the device has booted successfully, both *before and after* the user has authenticated with their credentials (such as a lock pattern or PIN).

Because device-protected data is available without user authentication, you should carefully limit the data you store using this Context. For example, storing sensitive authentication tokens or passwords in the device-protected area is strongly discouraged.

If the underlying device does not have the ability to store device-protected and credential-protected data using different keys, then both storage areas will become available at the same time. They remain as two distinct storage locations on disk, and only the window of availability changes.

Each call to this method returns a new instance of a Context object; Context objects are not shared, however common state (ClassLoader, other Resources for the same configuration) may be so the Context itself can be fairly lightweight.

Prior to API 24 this method returns `null`, since device-protected storage is not available.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#isDeviceProtectedStorage(android.content.Context)` |   |

### getAttributionTag

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

```
java-static fun getAttributionTag(context: Context): String?
```

Attribution can be used in complex apps to logically separate parts of the app. E.g. a blogging app might also have a instant messaging app built in. In this case two separate tags can for used each sub-feature.

Compatibility behavior:

- API 30 and above, returns the attribution tag or `null`
- API 29 and earlier, returns `null`

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | the attribution tag this context is for or `null` if this is the default. |

### getCodeCacheDir

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

```
java-static fun getCodeCacheDir(context: Context): File
```

Returns the absolute path to the application specific cache directory on the filesystem designed for storing cached code. On devices running `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#LOLLIPOP` or later, the system will delete any files stored in this location both when your specific application is upgraded, and when the entire platform is upgraded.

This location is optimal for storing compiled or optimized code generated by your application at runtime.

Apps require no extra permissions to read or write to the returned path, since this path lives in their private storage.

| Returns |
|---|---|
| `https://developer.android.com/reference/java/io/File.html` | The path of the directory holding application code cache files. |

### getColor

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

```
java-static fun getColor(context: Context, id: @ColorRes Int): @ColorInt Int
```

Returns a color associated with a particular resource ID

Starting in `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#M`, the returned color will be styled for the specified Context's theme.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | context to use for getting the color. |
| `id: @https://developer.android.com/reference/kotlin/androidx/annotation/ColorRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/ColorInt https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | A single color value in the form 0xAARRGGBB. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | if the given ID does not exist. |

### getColorStateList

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

```
java-static fun getColorStateList(context: Context, id: @ColorRes Int): ColorStateList?
```

Returns a color state list associated with a particular resource ID.

Starting in `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#M`, the returned color state list will be styled for the specified Context's theme.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | context to use for getting the color state list. |
| `id: @https://developer.android.com/reference/kotlin/androidx/annotation/ColorRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/res/ColorStateList.html?` | A color state list, or `null` if the resource could not be resolved. |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException.html` | if the given ID does not exist. |

### getContextForLanguage

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

```
java-static fun getContextForLanguage(context: Context): Context
```

Gets the context which respects the per-app locales locale. This API is specifically for developers who set the per-app locales via `https://developer.android.com/reference/kotlin/androidx/appcompat/app/AppCompatDelegate#setApplicationLocales(androidx.core.os.LocaleListCompat)`, but who needs to use the context out of `https://developer.android.com/reference/kotlin/androidx/appcompat/app/AppCompatActivity` scope.

The developers can override the returned context in Application's `https://developer.android.com/reference/android/content/ContextWrapper.html#attachBaseContext(android.content.Context)`, so that developers can get the localized string via application's context.

Compatibility behavior:

- API 17 and above, the locale in the context returned by this method will respect the the per-app locale.
- API 16 and earlier, this method directly return the `https://developer.android.com/reference/android/content/Context.html`

### getDataDir

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

```
java-static fun getDataDir(context: Context): File?
```

Returns the absolute path to the directory on the filesystem where all private files belonging to this app are stored. Apps should not use this path directly; they should instead use `https://developer.android.com/reference/android/content/Context.html#getFilesDir()`, `https://developer.android.com/reference/android/content/Context.html#getCacheDir()`, `https://developer.android.com/reference/android/content/Context.html#getDir(java.lang.String, int)`, or other storage APIs on `https://developer.android.com/reference/android/content/Context.html`.

The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

No additional permissions are required for the calling app to read or write files under the returned path.

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/pm/ApplicationInfo.html#dataDir` |   |

### getDisplayOrDefault

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

```
java-static fun getDisplayOrDefault(context: @DisplayContext Context): Display
```

Get the display this context is associated with or the `https://developer.android.com/reference/android/view/Display.html#DEFAULT_DISPLAY` as the fallback if the context is not associated with any `https://developer.android.com/reference/android/view/Display.html`.

Applications must use this method with `https://developer.android.com/reference/android/app/Activity.html` or a context associated with a `https://developer.android.com/reference/android/view/Display.html` via `https://developer.android.com/reference/android/content/Context.html#createDisplayContext(android.view.Display)` or `https://developer.android.com/reference/android/content/Context.html#createWindowContext(android.view.Display, int, android.os.Bundle)`, or the reported `https://developer.android.com/reference/android/view/Display.html` instance is not reliable.

| Parameters |
|---|---|
| `context: @https://developer.android.com/reference/kotlin/androidx/annotation/DisplayContext https://developer.android.com/reference/android/content/Context.html` | Context to obtain the associated display |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/Display.html` | The display associated with the Context or the default display if the context doesn't associated with any display. |

### getDrawable

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

```
java-static fun getDrawable(context: Context, id: @DrawableRes Int): Drawable?
```

Returns a drawable object associated with a particular resource ID.

Starting in `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#LOLLIPOP`, the returned drawable will be styled for the specified Context's theme.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | context to use for getting the drawable. |
| `id: @https://developer.android.com/reference/kotlin/androidx/annotation/DrawableRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/graphics/drawable/Drawable.html?` | Drawable An object that can be used to draw this resource. |

### getExternalCacheDirs

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

```
@ReplaceWith(expression = "context.getExternalCacheDirs()")
java-static fun [getExternalCacheDirs](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getExternalCacheDirs(android.content.Context))(context: Context): Array<File!>
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Call `https://developer.android.com/reference/android/content/Context.html#getExternalCacheDirs()` directly.

Returns absolute paths to application-specific directories on all external storage devices where the application can place cache files it owns. These files are internal to the application, and not typically visible to the user as media.

This is like `https://developer.android.com/reference/android/content/Context.html#getCacheDir()` in that these files will be deleted when the application is uninstalled, however there are some important differences:

- External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it.
- There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by `https://developer.android.com/reference/android/os/StatFs.html`.

Starting in `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#KITKAT`, no permissions are required to write to the returned paths; they're always accessible to the calling app. Before then, `https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE` is required to write. Write access outside of these paths on secondary external storage devices is not available. To request external storage access in a backwards compatible way, consider using `android:maxSdkVersion` like this:

```kotlin
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
```

The first path returned is the same as `https://developer.android.com/reference/android/content/Context.html#getExternalCacheDir()`. Returned paths may be `null` if a storage device is unavailable.

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#getExternalCacheDir()` |   |
| `https://developer.android.com/reference/kotlin/androidx/core/os/EnvironmentCompat#getStorageState(java.io.File)` |   |

### getExternalFilesDirs

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

```
@ReplaceWith(expression = "context.getExternalFilesDirs(type)")
java-static fun [getExternalFilesDirs](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getExternalFilesDirs(android.content.Context,java.lang.String))(context: Context, type: String?): Array<File!>
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Call getExternalFilesDirs directly.

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

This is like `https://developer.android.com/reference/android/content/Context.html#getFilesDir()` in that these files will be deleted when the application is uninstalled, however there are some important differences:

- External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it.
- There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by `https://developer.android.com/reference/android/os/StatFs.html`.

Starting in `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#KITKAT`, no permissions are required to write to the returned paths; they're always accessible to the calling app. Before then, `https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE` is required to write. Write access outside of these paths on secondary external storage devices is not available. To request external storage access in a backwards compatible way, consider using `android:maxSdkVersion` like this:

```kotlin
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
```

The first path returned is the same as `https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)`. Returned paths may be `null` if a storage device is unavailable.

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)` |   |
| `https://developer.android.com/reference/kotlin/androidx/core/os/EnvironmentCompat#getStorageState(java.io.File)` |   |

### getMainExecutor

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

```
java-static fun getMainExecutor(context: Context): Executor
```

Return an `https://developer.android.com/reference/java/util/concurrent/Executor.html` that will run enqueued tasks on the main thread associated with this context. This is the thread used to dispatch calls to application components (activities, services, etc).

### getNoBackupFilesDir

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

```
java-static fun getNoBackupFilesDir(context: Context): File?
```

Returns the absolute path to the directory on the filesystem similar to `https://developer.android.com/reference/android/content/Context.html#getFilesDir()`. The difference is that files placed under this directory will be excluded from automatic backup to remote storage on devices running `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#LOLLIPOP` or later.

No permissions are required to read or write to the returned path, since this path is internal storage.

| Returns |
|---|---|
| `https://developer.android.com/reference/java/io/File.html?` | The path of the directory holding application files that will not be automatically backed up to remote storage. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#getFilesDir()` |   |

### getObbDirs

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

```
@ReplaceWith(expression = "context.getObbDirs()")
java-static fun [getObbDirs](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#getObbDirs(android.content.Context))(context: Context): Array<File!>
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Call `https://developer.android.com/reference/android/content/Context.html#getObbDirs()` directly.

Returns absolute paths to application-specific directories on all external storage devices where the application's OBB files (if there are any) can be found. Note if the application does not have any OBB files, these directories may not exist.

This is like `https://developer.android.com/reference/android/content/Context.html#getFilesDir()` in that these files will be deleted when the application is uninstalled, however there are some important differences:

- External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it.
- There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by `https://developer.android.com/reference/android/os/StatFs.html`.

Starting in `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#KITKAT`, no permissions are required to write to the returned paths; they're always accessible to the calling app. Before then, `https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE` is required to write. Write access outside of these paths on secondary external storage devices is not available. To request external storage access in a backwards compatible way, consider using `android:maxSdkVersion` like this:

```kotlin
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
```

The first path returned is the same as `https://developer.android.com/reference/android/content/Context.html#getObbDir()`. Returned paths may be `null` if a storage device is unavailable.

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#getObbDir()` |   |
| `https://developer.android.com/reference/kotlin/androidx/core/os/EnvironmentCompat#getStorageState(java.io.File)` |   |

### getString

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

```
java-static fun getString(context: Context, resId: Int): String
```

Gets the resource string that also respects the per-app locales. If developers set the per-app locales via `https://developer.android.com/reference/kotlin/androidx/appcompat/app/AppCompatDelegate#setApplicationLocales(androidx.core.os.LocaleListCompat)`, this API returns localized strings even if the context is not `https://developer.android.com/reference/kotlin/androidx/appcompat/app/AppCompatActivity`.

Compatibility behavior:

- API 17 and above, this method return the localized string that respects per-app locales.
- API 16 and earlier, this method directly return the result of `https://developer.android.com/reference/android/content/Context.html#getString(int)`

### getSystemService

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

```
java-static fun <T> getSystemService(context: Context, serviceClass: Class<T!>): T?
```

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

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Context to retrieve service from. |
| `serviceClass: https://developer.android.com/reference/java/lang/Class.html<T!>` | The class of the desired service. |

| Returns |
|---|---|
| `T?` | The service or null if the class is not a supported system service. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.Class&lt;T&gt;)` |   |

### getSystemServiceName

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

```
java-static fun getSystemServiceName(context: Context, serviceClass: Class<*>): String?
```

Gets the name of the system-level service that is represented by the specified class.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Context to retrieve service name from. |
| `serviceClass: https://developer.android.com/reference/java/lang/Class.html<*>` | The class of the desired service. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The service name or null if the class is not a supported system service. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#getSystemServiceName(java.lang.Class&lt;?&gt;)` |   |

### isDeviceProtectedStorage

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

```
java-static fun isDeviceProtectedStorage(context: Context): Boolean
```

Indicates if the storage APIs of this Context are backed by device-encrypted storage.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#createDeviceProtectedStorageContext(android.content.Context)` |   |

### registerReceiver

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

```
java-static fun registerReceiver(
    context: Context,
    receiver: BroadcastReceiver?,
    filter: IntentFilter,
    flags: Int
): Intent?
```

Register a broadcast receiver.

| Parameters |
|---|---|
| `receiver: https://developer.android.com/reference/android/content/BroadcastReceiver.html?` | The BroadcastReceiver to handle the broadcast. |
| `context: https://developer.android.com/reference/android/content/Context.html` | Context to retrieve service from. |
| `filter: https://developer.android.com/reference/android/content/IntentFilter.html` | Selects the Intent broadcasts to be received. |
| `flags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | If this receiver is listening for broadcasts sent from other apps---even other apps that you own---use the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/core/content/ContextCompat#RECEIVER_NOT_EXPORTED()` flag. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html?` | The first sticky intent found that matches <var translate="no">filter</var>, or null if there are none. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, int)` |   |
| `https` | ://developer.android.com/develop/background-work/background-tasks/broadcasts#context-registered-receivers |

### registerReceiver

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

```
java-static fun registerReceiver(
    context: Context,
    receiver: BroadcastReceiver?,
    filter: IntentFilter,
    broadcastPermission: String?,
    scheduler: Handler?,
    flags: Int
): Intent?
```

Register a broadcast receiver.

| Parameters |
|---|---|
| `receiver: https://developer.android.com/reference/android/content/BroadcastReceiver.html?` | The BroadcastReceiver to handle the broadcast. |
| `context: https://developer.android.com/reference/android/content/Context.html` | Context to retrieve service from. |
| `filter: https://developer.android.com/reference/android/content/IntentFilter.html` | Selects the Intent broadcasts to be received. |
| `broadcastPermission: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | String naming a permission that a broadcaster must hold in order to send and Intent to you. If null, no permission is required. |
| `scheduler: https://developer.android.com/reference/android/os/Handler.html?` | Handler identifying the thread will receive the Intent. If null, the main thread of the process will be used. |
| `flags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | If this receiver is listening for broadcasts sent from other apps---even other apps that you own---use the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/core/content/ContextCompat#RECEIVER_NOT_EXPORTED()` flag. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html?` | The first sticky intent found that matches <var translate="no">filter</var>, or null if there are none. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter, java.lang.String, android.os.Handler, int)` |   |
| `https` | ://developer.android.com/develop/background-work/background-tasks/broadcasts#context-registered-receivers |

### startActivities

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

```
java-static fun startActivities(context: Context, intents: Array<Intent!>): Boolean
```

Start a set of activities as a synthesized task stack, if able.

In API level 11 (Android 3.0/Honeycomb) the recommended conventions for app navigation using the back key changed. The back key's behavior is local to the current task and does not capture navigation across different tasks. Navigating across tasks and easily reaching the previous task is accomplished through the "recents" UI, accessible through the software-provided Recents key on the navigation or system bar. On devices with the older hardware button configuration the recents UI can be accessed with a long press on the Home key.

When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.

startActivities provides a mechanism for constructing a synthetic task stack of multiple activities. If the underlying API is not available on the system this method will return false.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Start activities using this activity as the starting context |
| `intents: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/android/content/Intent.html!>` | Array of intents defining the activities that will be started. The element length-1 will correspond to the top activity on the resulting task stack. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the underlying API was available and the call was successful, false otherwise |

### startActivities

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

```
java-static fun startActivities(context: Context, intents: Array<Intent!>, options: Bundle?): Boolean
```

Start a set of activities as a synthesized task stack, if able.

In API level 11 (Android 3.0/Honeycomb) the recommended conventions for app navigation using the back key changed. The back key's behavior is local to the current task and does not capture navigation across different tasks. Navigating across tasks and easily reaching the previous task is accomplished through the "recents" UI, accessible through the software-provided Recents key on the navigation or system bar. On devices with the older hardware button configuration the recents UI can be accessed with a long press on the Home key.

When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.

startActivities provides a mechanism for constructing a synthetic task stack of multiple activities. If the underlying API is not available on the system this method will return false.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Start activities using this activity as the starting context |
| `intents: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/android/content/Intent.html!>` | Array of intents defining the activities that will be started. The element length-1 will correspond to the top activity on the resulting task stack. |
| `options: https://developer.android.com/reference/android/os/Bundle.html?` | Additional options for how the Activity should be started. See `https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent, android.os.Bundle)` |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the underlying API was available and the call was successful, false otherwise |

### startActivity

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

```
@ReplaceWith(expression = "context.startActivity(intent, options)")
java-static fun [startActivity](https://developer.android.com/reference/kotlin/androidx/core/content/ContextCompat#startActivity(android.content.Context,android.content.Intent,android.os.Bundle))(context: Context, intent: Intent, options: Bundle?): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Call startActivity directly.

Start an activity with additional launch information, if able.

In Android 4.1+ additional options were introduced to allow for more control on activity launch animations. Applications can use this method along with `https://developer.android.com/reference/kotlin/androidx/core/app/ActivityOptionsCompat` to use these animations when available. When run on versions of the platform where this feature does not exist the activity will be launched normally.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Context to launch activity from. |
| `intent: https://developer.android.com/reference/android/content/Intent.html` | The description of the activity to start. |
| `options: https://developer.android.com/reference/android/os/Bundle.html?` | Additional options for how the Activity should be started. May be null if there are no options. See `https://developer.android.com/reference/kotlin/androidx/core/app/ActivityOptionsCompat` for how to build the Bundle supplied here; there are no supported definitions for building it manually. |

### startForegroundService

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

```
java-static fun startForegroundService(context: Context, intent: Intent): Unit
```

startForegroundService() was introduced in O, just call startService for before O.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Context to start Service from. |
| `intent: https://developer.android.com/reference/android/content/Intent.html` | The description of the Service to start. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html#startForegroundService(android.content.Intent)` |   |
| `https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)` |   |