处理锻炼事件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
健康服务支持 ExerciseEvents
,它会在运动期间发生事件时通知您的应用并提供关联的元数据。
添加依赖项
使用运动事件时,需要最新版本的健康服务 SDK。
如需添加健康服务的依赖项,您必须将 Google Maven 制品库添加到项目中。如需了解相关信息,请参阅 Google 的 Maven 制品库。
然后在您的模块级 build.gradle
文件中,添加以下依赖项:
Groovy
dependencies {
implementation "androidx.health:health-services-client:1.1.0-alpha05"
}
Kotlin
dependencies {
implementation("androidx.health:health-services-client:1.1.0-alpha05")
}
检查功能
与健康服务中的所有运动和数据类型一样,请在启动时检查功能。尤其是对于 ExerciseEvents
,除了请求 ExerciseCapabilities
之外,应使用 ExerciseTypeCapabilities.supportedExerciseEvents
验证给定运动支持哪些运动事件。确认特定的 ExerciseEvent
受支持后,您还应使用 getExerciseEventCapabilityDetails
查询运动事件的功能。
以下示例展示了如何查询功能以确认 GOLF_SHOT_EVENT
是否受支持,然后确认 GOLF_SHOT_EVENT
是否支持摇摆类型分类。
fun handleCapabilities(capabilities: ExerciseCapabilities) {
val golfCapabilities = capabilities.typeToCapabilities[ExerciseType.GOLF]
val golfShotEventSupported =
golfCapabilities
?.supportedExerciseEvents
?.contains(ExerciseEventType.GOLF_SHOT_EVENT)
val golfSwingTypeClassificationSupported =
golfCapabilities
?.getExerciseEventCapabilityDetails(ExerciseEventType.GOLF_SHOT_EVENT)
?.isSwingTypeClassificationSupported ?: false
}
在运动时请求运动事件
如要开始运动并在运动时请求运动事件,请执行以下操作:为运动声明 ExerciseConfig
并为 exerciseEventType
添加一个字段。
在进行 GOLF
运动时,以下示例会请求 GOLF_SHOT_EVENT
:
val config = ExerciseConfig(
exerciseType = ExerciseType.GOLF,
dataTypes = setOf(....),
// ...
exerciseEventTypes = setOf(ExerciseEventType.GOLF_SHOT_EVENT),
)
注册运动事件更新
作为您的应用的现有基础架构的一部分,您可以通过接收 ExerciseEvent
更新来接收运动更新。以下示例展示了如何引入对 GolfShotEvent
更新的支持:
val callback = object : ExerciseUpdateCallback {
override fun onExerciseUpdateReceived(update: ExerciseUpdate) {
...
}
// [ExerciseEvent] intended to come through with low latency and out of
// band of onExerciseUpdateReceived()
override fun onExerciseEventReceived(event: ExerciseEvent) {
when (event) {
is GolfShotEvent -> {
if (it.swingType == GolfShotSwingType.PUTT) {
println("Putt detected!")
}
}
}
}
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Handle exercise events\n\nHealth Services provides support for [`ExerciseEvents`](/reference/kotlin/androidx/health/services/client/data/ExerciseEvent),\nwhich notify your app when an event has occurred during an exercise and supply associated metadata.\n\nAdd dependencies\n----------------\n\nUsing exercise events requires the latest version of the Health Services SDK.\n\nTo add a dependency on Health Services, you must add the Google Maven repository\nto your project. For more information, see\n[Google's Maven repository](/studio/build/dependencies#google-maven).\n\nThen, in your module-level `build.gradle` file, add the following dependency: \n\n### Groovy\n\n```groovy\ndependencies {\n implementation \"androidx.health:health-services-client:1.1.0-alpha05\"\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n implementation(\"androidx.health:health-services-client:1.1.0-alpha05\")\n}\n```\n\nCheck capabilities\n------------------\n\nAs with all exercises and data types in Health Services, [check capabilities at\nstartup](/training/wearables/health-services/active-data#capabilites). For\n[`ExerciseEvents`](/reference/kotlin/androidx/health/services/client/data/ExerciseEvent)\nin particular, in addition to requesting `ExerciseCapabilities`,\nuse [`ExerciseTypeCapabilities.supportedExerciseEvents`](/reference/androidx/health/services/client/data/ExerciseTypeCapabilities#getSupportedExerciseEvents())\nto verify which exercise events are supported for the given exercise.\nAfter confirming the particular `ExerciseEvent` is supported,\nyou should also query the capabilities of the exercise event using\n[`getExerciseEventCapabilityDetails`](/reference/androidx/health/services/client/data/ExerciseTypeCapabilities#getExerciseEventCapabilityDetails).\n\nThe following example shows how to query capabilities to confirm the\n`GOLF_SHOT_EVENT` is supported, and then confirm that the `GOLF_SHOT_EVENT`\nsupports Swing Type Classification. \n\n fun handleCapabilities(capabilities: ExerciseCapabilities) {\n val golfCapabilities = capabilities.typeToCapabilities[ExerciseType.GOLF]\n val golfShotEventSupported =\n golfCapabilities\n ?.supportedExerciseEvents\n ?.contains(ExerciseEventType.GOLF_SHOT_EVENT)\n val golfSwingTypeClassificationSupported =\n golfCapabilities\n ?.getExerciseEventCapabilityDetails(ExerciseEventType.GOLF_SHOT_EVENT)\n ?.isSwingTypeClassificationSupported ?: false\n }\n\nRequest exercise events in an exercise\n--------------------------------------\n\nTo start the exercise and request an exercise event as part of the exercise,\n[declare the `ExerciseConfig` for the exercise](/training/wearables/health-services/active-data#start)\nand add a field for [`exerciseEventType`](/reference/androidx/health/services/client/data/ExerciseEventType).\n\nThe following example requests `GOLF_SHOT_EVENT` as part of a `GOLF` exercise: \n\n val config = ExerciseConfig(\n exerciseType = ExerciseType.GOLF,\n dataTypes = setOf(....),\n // ...\n exerciseEventTypes = setOf(ExerciseEventType.GOLF_SHOT_EVENT),\n )\n\nRegister for exercise event updates\n-----------------------------------\n\nYou can receive `ExerciseEvent` updates as part of the existing infrastructure\nyour app has for [receiving exercise updates](/training/wearables/health-services/active-data#updates).\nThe following example shows how you would incorporate support for `GolfShotEvent` updates: \n\n val callback = object : ExerciseUpdateCallback {\n override fun onExerciseUpdateReceived(update: ExerciseUpdate) {\n ...\n }\n // [ExerciseEvent] intended to come through with low latency and out of\n // band of onExerciseUpdateReceived()\n override fun onExerciseEventReceived(event: ExerciseEvent) {\n when (event) {\n is GolfShotEvent -\u003e {\n if (it.swingType == GolfShotSwingType.PUTT) {\n println(\"Putt detected!\")\n }\n }\n }\n }\n }"]]