จัดการกิจกรรมการออกกำลังกาย
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บริการข้อมูลสุขภาพจะให้การสนับสนุนสำหรับ ExerciseEvents
ซึ่งจะแจ้งเตือนแอปของคุณเมื่อเกิดเหตุการณ์ขึ้นระหว่างการออกกำลังกายและให้ข้อมูลเมตาที่เกี่ยวข้อง
เพิ่มทรัพยากร Dependency
การใช้เหตุการณ์การออกกำลังกายต้องใช้ SDK บริการสุขภาพเวอร์ชันล่าสุด
หากต้องการเพิ่มทรัพยากร Dependency ของ Health Services คุณต้องเพิ่มที่เก็บ Google Maven
ลงในโปรเจ็กต์ของคุณ สำหรับข้อมูลเพิ่มเติม โปรดดู
ที่เก็บ Maven ของ Google
จากนั้นเพิ่มการอ้างอิงต่อไปนี้ในไฟล์ build.gradle
ระดับโมดูล
ดึงดูด
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_SHOT_EVENT
เป็นส่วนหนึ่งของแบบฝึกหัด GOLF
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 และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],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 }"]]