Android XR SDK 现已推出开发者预览版。我们期望收到您的反馈!请访问我们的
支持页面与我们联系。
检查空间功能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
空间功能可能会随着用户与您的应用或系统互动而发生变化,甚至可能会由您的应用本身更改(例如,进入 Home Space 或 Full Space)。为避免出现问题,您的应用需要检查空间功能,以确定当前环境支持哪些 API。
使用 Jetpack Compose for XR 检查空间功能
Jetpack Compose for XR 会创建一个 Composition Local 来检查空间功能。您可以使用此命令来检查是否已启用空间界面、空间音频、环境、透传或 3D 内容。
您可以使用 LocalSpatialCapabilities.current
检查以下空间功能目前是否可用:
以下示例展示了如何检查是否已启用空间界面:
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
Subspace {
SpatialPanel(
modifier = SubspaceModifier
.width(1488.dp)
.fillMaxHeight()
) {
AppContent()
}
}
} else {
AppContent()
}
使用 SceneCore 检查空间功能
使用 SceneCore 库时,您必须创建一个会话。创建会话后,请对会话调用 spatialCapabilities
以查询当前可用的空间功能。
您还可以选择订阅回调 addSpatialCapabilitiesChangedListener
,以便在空间功能发生变化时收到通知。
// Example 1: check if enabling passthrough mode is allowed
if (xrSession.scene.spatialCapabilities.hasCapability(
SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL
)
) {
xrSession.scene.spatialEnvironment.preferredPassthroughOpacity = 1f
}
// Example 2: multiple capability flags can be checked simultaneously:
if (xrSession.scene.spatialCapabilities.hasCapability(
SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and
SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT
)
) {
// ...
}
另请参阅
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-23。
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# Check for spatial capabilities\n\nSpatial capabilities can change as users interact with your app or the system,\nor can even be changed by your app itself---for example, moving into Home Space or\nFull Space. To avoid issues, your app needs to check for spatial capabilities to\ndetermine which APIs are supported in the current environment.\n\nCheck for spatial capabilities using Jetpack Compose for XR\n-----------------------------------------------------------\n\nJetpack Compose for XR creates a Composition Local for checking spatial\ncapabilities. Use this to check whether spatial UI, spatial audio, environments,\npassthrough, or 3D content is enabled.\n\nYou can use [`LocalSpatialCapabilities.current`](/reference/kotlin/androidx/xr/compose/platform/package-summary#LocalSpatialCapabilities()) to check if the following\nspatial capabilities are currently available:\n\n- [`isSpatialUiEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isSpatialUiEnabled()): Indicates whether the application may create spatial UI elements (for example, `SpatialPanel`).\n- [`isContent3dEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isContent3dEnabled()): Indicates whether the application may create 3D objects.\n- [`isAppEnvironmentEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isAppEnvironmentEnabled()): Indicates whether the application may set the environment.\n- [`isPassthroughControlEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isPassthroughControlEnabled()): Indicates whether the application may control the passthrough state.\n- [`isSpatialAudioEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isSpatialAudioEnabled()): Indicates whether the application may use spatial audio.\n\nThe following example shows how to check if spatial UI is enabled:\n\n\n```kotlin\nif (LocalSpatialCapabilities.current.isSpatialUiEnabled) {\n Subspace {\n SpatialPanel(\n modifier = SubspaceModifier\n .width(1488.dp)\n .fillMaxHeight()\n ) {\n AppContent()\n }\n }\n} else {\n AppContent()\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/xr/src/main/java/com/example/xr/compose/SpatialCapabilities.kt#L51-L63\n```\n\n\u003cbr /\u003e\n\nCheck for spatial capabilities using SceneCore\n----------------------------------------------\n\nWhen using the SceneCore library, you'll have to create a [session](/develop/xr/jetpack-xr-sdk/add-session). Once the\nsession is created, call [`spatialCapabilities`](/reference/kotlin/androidx/xr/scenecore/Scene#spatialCapabilities()) on the session to query\nwhich spatial capabilities are currently available.\n\n- [`SPATIAL_CAPABILITY_3D_CONTENT`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_3D_CONTENT()): The activity can create 3D contents.\n- [`SPATIAL_CAPABILITY_APP_ENVIRONMENT`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_APP_ENVIRONMENT()): The activity can set its own environment.\n- [`SPATIAL_CAPABILITY_EMBED_ACTIVITY`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_EMBED_ACTIVITY()): The activity can spatially embed another activity.\n- [`SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL()): The activity can enable or disable passthrough.\n- [`SPATIAL_CAPABILITY_SPATIAL_AUDIO`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_SPATIAL_AUDIO()): The activity can use spatial audio.\n- [`SPATIAL_CAPABILITY_UI`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_UI()): The activity can spatialize itself (for example, adding a spatial panel).\n\nYou can also choose to subscribe to a callback,\n[`addSpatialCapabilitiesChangedListener`](/reference/kotlin/androidx/xr/scenecore/Scene#addSpatialCapabilitiesChangedListener(java.util.function.Consumer)) that notifies you when spatial\ncapabilities have changed.\n\n\n```kotlin\n// Example 1: check if enabling passthrough mode is allowed\nif (xrSession.scene.spatialCapabilities.hasCapability(\n SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL\n )\n) {\n xrSession.scene.spatialEnvironment.preferredPassthroughOpacity = 1f\n}\n// Example 2: multiple capability flags can be checked simultaneously:\nif (xrSession.scene.spatialCapabilities.hasCapability(\n SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and\n SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT\n )\n) {\n // ...\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/xr/src/main/java/com/example/xr/scenecore/SpatialCapabilities.kt#L25-L39\n```\n\n\u003cbr /\u003e\n\nSee also\n--------\n\n- [Create a session](/develop/xr/jetpack-xr-sdk/check-spatial-capabilities)\n- [Transition between HSM and FSM](/develop/xr/jetpack-xr-sdk/transition-home-space-to-full-space)\n- [Add spatial environments to your app](/develop/xr/jetpack-xr-sdk/add-environments)\n- [Add 3D models to your app](/develop/xr/jetpack-xr-sdk/add-3d-models)"]]