ניהול אירועי תרגילים

שירותי הבריאות מספקים תמיכה עבור ExerciseEvents, שמיידעות את האפליקציה כשמתרחש אירוע במהלך פעילות גופנית, עם מטא-נתונים משויכים.

הוספת יחסי תלות

כדי להשתמש באירועי אימון כושר, נדרשת הגרסה העדכנית של Health Services SDK.

כדי להוסיף תלות בשירותי Health, צריך להוסיף את מאגר Google Maven לפרויקט שלך. מידע נוסף זמין במאמר הבא: מאגר Maven של Google.

לאחר מכן, בקובץ build.gradle ברמת המודול, מוסיפים את התלות הבאה:

Groovy

dependencies {
    implementation "androidx.health:health-services-client:1.1.0-alpha03"
}

Kotlin

dependencies {
    implementation("androidx.health:health-services-client:1.1.0-alpha03")
}

בדיקת היכולות

כמו בכל התרגילים וסוגי הנתונים בשירותי הבריאות, כדאי לבדוק את היכולות כאן הפעלה. עבור 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!")
        }
      }
    }
  }
}