# 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
public final class ProjectedActivityCompat implements AutoCloseable
```

<br />

*** ** * ** ***

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

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

## Summary

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#clearActivityAsInputReceiver()()` Clears the Activity registered for input via `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#setActivityAsInputReceiver(android.app.PendingIntent)`. |
| `void` | `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#close()()` |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#create(android.app.Activity)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity)` Connects to the service providing features for Projected devices and returns the ProjectedActivityCompat when the connection is established. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat` | `[create](https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` **This method is deprecated.** Use create(activity: Activity) instead |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedInputEvent>` | `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#getProjectedInputEvents()()` Flow providing a stream of Projected input events. |
| `static final void` | `@https://developer.android.com/reference/androidx/annotation/WorkerThread @https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 35) https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#requestPermissions(android.app.Activity,kotlin.Array,kotlin.Int)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity, @https://developer.android.com/reference/androidx/annotation/NonNull String[] permissions, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int requestCode )` Requests permissions to be granted to this application. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#setActivityAsInputReceiver(android.app.PendingIntent)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/PendingIntent.html intent)` 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 methods

### clearActivityAsInputReceiver

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

```
public final void clearActivityAsInputReceiver()
```

Clears the Activity registered for input via `https://developer.android.com/reference/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)

```
public void close()
```

### create

```
@RequiresApi(value = 34)
public static final @NonNull ProjectedActivityCompat create(@NonNull Activity activity)
```

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

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat` | A `https://developer.android.com/reference/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

```
public static final @NonNull ProjectedActivityCompat [create](https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context))(@NonNull Context context)
```

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

Connects to the service providing features for Projected devices and returns the `https://developer.android.com/reference/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The context to use for binding to the service. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat` | A `https://developer.android.com/reference/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 |

### getProjectedInputEvents

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

```
public final @NonNull Flow<@NonNull ProjectedInputEvent> getProjectedInputEvents()
```

Flow providing a stream of Projected input events.

### requestPermissions

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

```
@WorkerThread
@RequiresApi(value = 35)
public static final void requestPermissions(
    @NonNull Activity activity,
    @NonNull String[] permissions,
    @IntRange(from = 0) int requestCode
)
```

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 |

### setActivityAsInputReceiver

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

```
public final void setActivityAsInputReceiver(@NonNull PendingIntent intent)
```

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:

```
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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/PendingIntent.html intent` | 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` |