رویدادهای ورزشی را مدیریت کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
Health Services از ExerciseEvents
پشتیبانی میکند که در صورت وقوع رویدادی در حین تمرین به برنامه شما اطلاع میدهد و ابردادههای مرتبط را عرضه میکند.
وابستگی ها را اضافه کنید
استفاده از رویدادهای ورزشی به آخرین نسخه SDK خدمات سلامت نیاز دارد.
برای افزودن وابستگی به خدمات سلامت، باید مخزن Google Maven را به پروژه خود اضافه کنید. برای اطلاعات بیشتر، به مخزن Maven Google مراجعه کنید.
سپس، در فایل build.gradle
در سطح ماژول، وابستگی زیر را اضافه کنید:
شیار
dependencies {
implementation "androidx.health:health-services-client:1.1.0-alpha05"
}
کاتلین
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!")
}
}
}
}
}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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 }"]]