检查功能可用性
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
当健康数据共享添加新功能时,用户可能不会总是更新其健康数据共享版本。借助功能可用性 API,您可以检查健康数据共享中的某项功能是否在用户的设备上可用,并决定要采取的操作。
开始使用
功能可用性 API 与健康数据共享 SDK 共享相同的依赖项。首先,请验证您的 build.gradle
文件中是否包含至少版本 1.1.0-alpha08
:
dependencies {
implementation("androidx.health.connect:connect-client:1.1.0-alpha08")
}
用于检查功能可用性的主要函数是 getFeatureStatus()
。此方法会返回整数常量 FEATURE_STATUS_AVAILABLE
或 FEATURE_STATUS_UNAVAILABLE
:
if (healthConnectClient
.features
.getFeatureStatus(
HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_IN_BACKGROUND
) == HealthConnectFeatures.FEATURE_STATUS_AVAILABLE) {
// Feature is available
...
} else {
// Feature is not available
...
}
如需查看所有可用功能标志的列表,请参阅HealthConnectFeatures
参考页面。
处理功能不可用的情况
如果某项功能在用户的设备上不可用,更新可能会启用该功能。如果用户设备上安装的健康数据共享不是最新支持的版本,您可以考虑引导用户更新健康数据共享。不过,使用 APK(在 Android 13 及更低版本上)的用户无法使用仅在搭载 Android 14 或更高版本的设备上提供的系统模块功能。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-23。
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# Check for feature availability\n\nWhen new features are added to Health Connect, users may not always update their\nversion of Health Connect. The Feature Availability API is a way to check if a\nfeature in Health Connect is available on your user's device and decide what\naction to take.\n\nGet started\n-----------\n\nThe Feature Availability API shares the same dependency as the Health Connect\nSDK. To get started, verify that at least version `1.1.0-alpha08` is in your\n`build.gradle` file: \n\n dependencies {\n implementation(\"androidx.health.connect:connect-client:1.1.0-alpha08\")\n }\n\nPerform the check\n-----------------\n\nThe main function to check for feature availability is `getFeatureStatus()`.\nThis returns integer constants `FEATURE_STATUS_AVAILABLE` or\n`FEATURE_STATUS_UNAVAILABLE`: \n\n if (healthConnectClient\n .features\n .getFeatureStatus(\n HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_IN_BACKGROUND\n ) == HealthConnectFeatures.FEATURE_STATUS_AVAILABLE) {\n\n // Feature is available\n ...\n } else {\n // Feature is not available\n ...\n }\n\nFor a list of all available feature flags, see the [`HealthConnectFeatures`](/reference/androidx/health/connect/client/HealthConnectFeatures)\nreference page.\n\nHandle lack of feature availability\n-----------------------------------\n\nIf a feature isn't available on a user's device, an update may enable it. You\nmay consider directing the user to update Health Connect if they don't have\nthe latest supported version on their device. However, users using the APK\n(on Android 13 and lower) can't use the system module features that are only\navailable on devices running Android 14 or higher."]]