التعامل مع أحداث التمارين
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
توفر "الخدمات الصحية" الدعم لـ ExerciseEvents
،
التي ترسل إشعارًا إلى تطبيقك عند وقوع حدث أثناء ممارسة تمرين، وتوفّر البيانات الوصفية المرتبطة بها.
إضافة التبعيات
يتطلّب استخدام أحداث التمارين الرياضية أحدث إصدار من "حزمة تطوير البرامج (SDK) للخدمات الصحية".
لإضافة الاعتماد على "الخدمات الصحية"، يجب إضافة مستودع 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 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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 }"]]