# ProjectedContext

Artifact: [androidx.xr.projected:projected](https://developer.android.com/jetpack/androidx/releases/xr-projected) [View Source](https://cs.android.com/search?q=file:androidx/xr/projected/ProjectedContext.kt+class:androidx.xr.projected.ProjectedContext) Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

*** ** * ** ***


```
@ExperimentalProjectedApi
object ProjectedContext
```

<br />

*** ** * ** ***

Helper for accessing Projected device `https://developer.android.com/reference/android/content/Context.html` and its features.

Projected device is an XR device connected to an Android device (host). Host can project the application content to the Projected device and let users interact with it.

The Projected device context will ensure Projected device system services are returned, when queried for system services from this object.

Note: The application context's deviceId can switch between the Projected and host deviceId depending on which activity was most recently in the foreground. Prefer using the Activity context to minimize the risk of running into this problem.

## Summary

| ### Public functions |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#createHostDeviceContext(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Explicitly create the host device context from any context object. |
| `https://developer.android.com/reference/android/app/ActivityOptions.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 35) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#createProjectedActivityOptions(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Creates `https://developer.android.com/reference/android/app/ActivityOptions.html` that should be used to start an activity on the Projected device. |
| `https://developer.android.com/reference/android/content/Context.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#createProjectedDeviceContext(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Explicitly create the Projected device context from any context object. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#getProjectedDeviceName(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Returns the name of the Projected device or null if either virtual device wasn't found or the name of the virtual device wasn't set. |
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 36) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#isProjectedDeviceConnected(android.content.Context,kotlin.coroutines.CoroutineContext)( context: https://developer.android.com/reference/android/content/Context.html, coroutineContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` Observe whether a Projected device is connected to the host. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#isProjectedDeviceContext(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Returns whether the provided context is the Projected device context. |

## Public functions

### createHostDeviceContext

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
fun createHostDeviceContext(context: Context): Context
```

Explicitly create the host device context from any context object. The host is the device that connects to a Projected device.

If an application is using a Projected device context and it wants to use system services from the host (e.g. phone), it needs to use the host device context.

### createProjectedActivityOptions

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 35)
fun createProjectedActivityOptions(context: Context): ActivityOptions
```

Creates `https://developer.android.com/reference/android/app/ActivityOptions.html` that should be used to start an activity on the Projected device.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | any `https://developer.android.com/reference/android/content/Context.html` object. If the provided context is not a Projected device context, a Projected device context will be created automatically and used to create Projected `https://developer.android.com/reference/android/app/ActivityOptions.html`. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the projected display was not found or if the Projected device doesn't have any displays. |

### createProjectedDeviceContext

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
fun createProjectedDeviceContext(context: Context): Context
```

Explicitly create the Projected device context from any context object.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the projected device was not found. |

### getProjectedDeviceName

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
fun getProjectedDeviceName(context: Context): String?
```

Returns the name of the Projected device or null if either virtual device wasn't found or the name of the virtual device wasn't set.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | If another context is used (e.g. the host context). |

### isProjectedDeviceConnected

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 36)
fun isProjectedDeviceConnected(
    context: Context,
    coroutineContext: CoroutineContext
): Flow<Boolean>
```

Observe whether a Projected device is connected to the host.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The context used to access the `https://developer.android.com/reference/android/companion/virtual/VirtualDeviceManager.html`. It can be any context object. |
| `coroutineContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` | The CoroutineContext that includes CoroutineDispatcher which is where the Projected device connectivity observer is executed on. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if provided coroutineContext doesn't include CoroutineDispatcher. |

### isProjectedDeviceContext

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
fun isProjectedDeviceContext(context: Context): Boolean
```

Returns whether the provided context is the Projected device context.