שירותי הבריאות מספקים תמיכה ב-ExerciseEvents
, שמודיעים לאפליקציה שלכם כשאירוע מתרחש במהלך פעילות גופנית ומספקים מטא-נתונים משויכים.
הוספת יחסי תלות
כדי להשתמש באירועי פעילות גופנית, צריך להשתמש בגרסה האחרונה של Health Services SDK.
כדי להוסיף תלות ב-Health Services, צריך להוסיף את מאגר Google Maven לפרויקט. מידע נוסף זמין במאמר בנושא מאגר Maven של Google.
לאחר מכן, בקובץ 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") }
בדיקת היכולות
כמו בכל התרגילים וסוגי הנתונים ב-Health Services, חשוב לבדוק את היכולות בהפעלה. במקרה של
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!")
}
}
}
}
}