ProjectedActivityCompat


@ExperimentalProjectedApi
class ProjectedActivityCompat : AutoCloseable


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

Use create to create an instance of this class. Use close to clear the instance.

Summary

Public companion functions

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

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

suspend ProjectedActivityCompat
create(context: Context)

This function is deprecated. Use create(activity: Activity) instead

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

Requests permissions to be granted to this application.

Public functions

Unit

Clears the Activity registered for input via setActivityAsInputReceiver.

open Unit
Unit

Registers a PendingIntent to indicate which Activity should receive inputs when the application becomes user-aware (like playing audio or using Projected Context data streams).

Public properties

Flow<ProjectedInputEvent>

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: Activity

The Activity running on a Projected device

Throws
IllegalStateException

if the projected service is not found or binding is not permitted

IllegalArgumentException

if provided Activity is not running on a Projected device

create

suspend fun create(context: Context): ProjectedActivityCompat

Connects to the service providing features for Projected devices and returns the 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: Context

The context to use for binding to the service.

Throws
IllegalStateException

if the projected service is not found or binding is not permitted

requestPermissions

Added in 1.0.0-alpha13
@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 Activity, 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 Activity.onRequestPermissionsResult method overridden by the activity passed to this method.

Throws
IllegalStateException

if the projected service is not found or binding is not permitted

IllegalArgumentException

if provided Activity is not running on a Projected device

Public functions

clearActivityAsInputReceiver

Added in 1.0.0-alpha13
fun clearActivityAsInputReceiver(): Unit

Clears the Activity registered for input via setActivityAsInputReceiver.

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.

close

Added in 1.0.0-alpha13
open fun close(): Unit

setActivityAsInputReceiver

Added in 1.0.0-alpha13
fun setActivityAsInputReceiver(intent: PendingIntent): Unit

Registers a PendingIntent to indicate which Activity 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.

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.

  • Even if the application is already in the foreground without user awareness, the platform will still send the provided PendingIntent when it becomes perceptible, regardless of whether the component in the PendingIntent exactly matches the foreground component.

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
intent: PendingIntent

The PendingIntent to route inputs to

Throws
IllegalArgumentException

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-alpha13
val projectedInputEventsFlow<ProjectedInputEvent>

Flow providing a stream of Projected input events.