# ProjectedActivityCompat

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/ProjectedActivityCompat.kt+class:androidx.xr.projected.ProjectedActivityCompat) Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

*** ** * ** ***


```
@ExperimentalProjectedApi
class ProjectedActivityCompat : AutoCloseable
```

<br />

*** ** * ** ***

Class providing Projected device-specific features for Activity, like listening to Projected input events.

Use `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context)` to create an instance of this class. Use `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#close()` to clear the instance.

## Summary

| ### Public companion functions |
|---|---|
| `suspend https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#create(android.app.Activity)(activity: https://developer.android.com/reference/android/app/Activity.html)` Connects to the service providing features for Projected devices and returns the ProjectedActivityCompat when the connection is established. |
| `suspend https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat` | `[create](https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context))(context: https://developer.android.com/reference/android/content/Context.html)` **This function is deprecated.** Use create(activity: Activity) instead |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread @https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 35) https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#requestPermissions(android.app.Activity,kotlin.Array,kotlin.Int)( activity: https://developer.android.com/reference/android/app/Activity.html, permissions: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>, requestCode: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Requests permissions to be granted to this application. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#clearActivityAsInputReceiver()()` Clears the Activity registered for input via `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#setActivityAsInputReceiver(android.app.PendingIntent)`. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#close()()` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#setActivityAsInputReceiver(android.app.PendingIntent)(intent: https://developer.android.com/reference/android/app/PendingIntent.html)` Registers a `https://developer.android.com/reference/android/app/PendingIntent.html` to indicate which `https://developer.android.com/reference/android/app/Activity.html` should receive inputs when the application becomes user-aware (like playing audio or using Projected Context data streams). |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedInputEvent>` | `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#projectedInputEvents()` Flow providing a stream of Projected input events. |

## Public companion functions

### create

```
@RequiresApi(value = 34)
suspend fun create(activity: Activity): ProjectedActivityCompat
```

Connects to the service providing features for Projected devices and returns the ProjectedActivityCompat when the connection is established.

| Parameters |
|---|---|
| `activity: https://developer.android.com/reference/android/app/Activity.html` | The `https://developer.android.com/reference/android/app/Activity.html` running on a Projected device |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat` | A `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat` instance |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the projected service is not found or binding is not permitted |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if provided Activity is not running on a Projected device |

### create

```
suspend fun [create](https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context))(context: Context): ProjectedActivityCompat
```

> [!CAUTION]
> **This function is deprecated.**   
> Use create(activity: Activity) instead

Connects to the service providing features for Projected devices and returns the `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat` when the connection is established.

Use ProjectedActivityCompat.create(activity: Activity) instead

TODO(b/479214966): Remove this once all clients have been migrated.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The context to use for binding to the service. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat` | A `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat` instance. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the projected service is not found or binding is not permitted |

### requestPermissions

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

```
@WorkerThread
@RequiresApi(value = 35)
fun requestPermissions(
    activity: Activity,
    permissions: Array<String>,
    requestCode: @IntRange(from = 0) Int
): Unit
```

Requests permissions to be granted to this application. These permissions must be declared in your manifest, they should not be granted to your app, and they should have protection level dangerous, regardless of whether they are declared by the platform or a third-party app.

When this method is called from a Projected `https://developer.android.com/reference/android/app/Activity.html`, a new activity will be launched on the Projected device and another activity will be launched on the host device (e.g. phone). The Projected activity will request the user to go to the host activity to act on the permission request. The host activity will request permissions on behalf of the calling application and the system dialog for permission requests will appear for the user to grant/deny the permission.

After the user has acted on the permission request, both the Projected activity and host activity will finish and the results will be delivered to the `https://developer.android.com/reference/android/app/Activity.html#onRequestPermissionsResult(int, java.lang.String[], int[])` method overridden by the activity passed to this method.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the projected service is not found or binding is not permitted |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if provided Activity is not running on a Projected device |

## Public functions

### clearActivityAsInputReceiver

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

```
fun clearActivityAsInputReceiver(): Unit
```

Clears the Activity registered for input via `https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedActivityCompat#setActivityAsInputReceiver(android.app.PendingIntent)`.

**Note:** This API is designed for audio Projected experiences. On devices with a visual display, calls to this method have no effect. For more information, see [types of Android XR devices](https://developer.android.com/develop/xr/devices#audio-display).

### close

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

```
open fun close(): Unit
```

### setActivityAsInputReceiver

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

```
fun setActivityAsInputReceiver(intent: PendingIntent): Unit
```

Registers a `https://developer.android.com/reference/android/app/PendingIntent.html` to indicate which `https://developer.android.com/reference/android/app/Activity.html` should receive inputs when the application becomes user-aware (like playing audio or using Projected Context data streams).

**Note:** This API is designed for audio Projected experiences. On devices with a visual display, calls to this method have no effect. For more information, see [types of Android XR devices](https://developer.android.com/develop/xr/devices#audio-display).

The platform enforces the following runtime guarantees for this receiver:

- The application will not be relaunched as an input handler unless it has active audio playback or a data stream and becomes user-aware.

- The registered input receiver is automatically cleared when the Projected device is disconnected or no longer worn.

- If the application is already in the foreground without user awareness, the platform will not send the provided `https://developer.android.com/reference/android/app/PendingIntent.html` when it becomes user-aware.

Usage example:

```kotlin
val controller = ProjectedActivityCompat.create(this)
val intent = Intent(this, NavigationControlsActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
    this,
    0,
    intent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
controller.setActivityAsInputReceiver(pendingIntent)
```

| Parameters |
|---|---|
| `intent: https://developer.android.com/reference/android/app/PendingIntent.html` | The `https://developer.android.com/reference/android/app/PendingIntent.html` to route inputs to |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if the provided PendingIntent is not for a component with category `android.hardware.display.category.XR_PROJECTED` |

## Public properties

### projectedInputEvents

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

```
val projectedInputEvents: Flow<ProjectedInputEvent>
```

Flow providing a stream of Projected input events.