קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
עם
MeasureClient
API, האפליקציה רושמת קריאות חוזרות (callback) כדי לקבל נתונים לפרק זמן קצר.
האפשרות הזו מיועדת למצבים שבהם האפליקציה נמצאת בשימוש ונדרשים לה נתונים במהירות
אם אפשר, כדאי ליצור קטע כזה עם ממשק משתמש שפועל בחזית כדי שהמשתמש
מתוך מודעות.
הוספת יחסי תלות
כדי להוסיף תלות בשירותי Health, צריך להוסיף את מאגר Google Maven
לפרויקט שלך. מידע נוסף זמין במאמר הבא:
מאגר Maven של Google.
לאחר מכן, בקובץ build.gradle ברמת המודול, מוסיפים את התלות הבאה:
לפני הרישום לעדכוני נתונים, חשוב לוודא שהמכשיר יכול לספק את סוג
של הנתונים שהאפליקציה צריכה. קודם כל בדיקת היכולות מאפשרת להפעיל
להשבית תכונות מסוימות או לשנות את ממשק המשתמש של האפליקציה כדי לפצות על היכולות
שאינם זמינים.
הדוגמה הבאה מראה איך לבדוק אם מכשיר יכול לספק את
סוג הנתונים HEART_RATE_BPM:
כל קריאה חוזרת (callback) שרושמים מיועדת לסוג נתונים יחיד. שימו לב שחלק מסוגי הנתונים
יכולים להיות מצבי זמינות שונים. לדוגמה, ייתכן שנתוני הדופק לא
להיות זמין כאשר המכשיר לא מחובר כראוי לפרק כף היד.
חשוב לצמצם את משך הזמן שבו מתבצע רישום הקריאה החוזרת,
קריאות חוזרות (callback) גורמות לעלייה בקצב הדגימה של החיישנים, וכתוצאה מכך עולה
צריכת חשמל.
הדוגמה הבאה מראה איך לרשום ולבטל את הרישום של קריאה חוזרת כדי לקבל
הנתונים של HEART_RATE_BPM:
valheartRateCallback=object:MeasureCallback{overridefunonAvailabilityChanged(dataType:DeltaDataType<*,*>,availability:Availability){if(availabilityisDataTypeAvailability){// Handle availability change.}}overridefunonDataReceived(data:DataPointContainer){// Inspect data points.}}valhealthClient=HealthServices.getClient(this/*context*/)valmeasureClient=healthClient.measureClient// Register the callback.measureClient.registerMeasureCallback(DataType.Companion.HEART_RATE_BPM,heartRateCallback)// Unregister the callback.awaitClose{runBlocking{measureClient.unregisterMeasureCallbackAsync(DataType.Companion.HEART_RATE_BPM,heartRateCallback)}}
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. 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,["# Take spot health measurements with MeasureClient\n\nWith the\n[`MeasureClient`](/reference/androidx/health/services/client/MeasureClient)\nAPI, your app registers callbacks to receive data for a short amount of time.\nThis is meant for situations in which your app is in use and requires rapid data\nupdates. If possible, create this with a foreground UI so that the user is\naware.\n| **Note:** `MeasureClient` is not suitable for workout tracking. Instead, [record an\n| exercise](/training/wearables/health-services/active-data/exercise-client) using `ExerciseClient`.\n\nAdd dependencies\n----------------\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| **Note:** This API is asynchronous and relies on `ListenableFuture` extensively. See [Using a ListenableFuture](/guide/background/listenablefuture) for more information about this concept.\n\nCheck capabilities\n------------------\n\nBefore registering for data updates, check that the device can provide the type\nof data your app needs. By checking capabilities first, you can enable or\ndisable certain features or modify your app's UI to compensate for capabilities\nthat are not available.\n\nThe following example shows how to check whether a device can provide the\n`HEART_RATE_BPM` data type: \n\n val healthClient = HealthServices.getClient(this /*context*/)\n val measureClient = healthClient.measureClient\n lifecycleScope.launch {\n val capabilities = measureClient.getCapabilitiesAsync().await()\n supportsHeartRate = DataType.HEART_RATE_BPM in capabilities.supportedDataTypesMeasure\n }\n\nRegister for data\n-----------------\n\n| **Note:** Request necessary permissions before registering to receive data that requires a permission.\n\nEach callback you register is for a single data type. Note that some data types\nmight have varying states of availability. For example, heart rate data might not\nbe available when the device is not properly attached to the wrist.\n\nIt's important to minimize the amount of time that your callback is registered,\nas callbacks cause an increase in sensor sampling rates, which in turn increases\npower consumption.\n\nThe following example shows how to register and unregister a callback to receive\n`HEART_RATE_BPM` data: \n\n val heartRateCallback = object : MeasureCallback {\n override fun onAvailabilityChanged(dataType: DeltaDataType\u003c*, *\u003e, availability: Availability) {\n if (availability is DataTypeAvailability) {\n // Handle availability change.\n }\n }\n\n override fun onDataReceived(data: DataPointContainer) {\n // Inspect data points.\n }\n }\n val healthClient = HealthServices.getClient(this /*context*/)\n val measureClient = healthClient.measureClient\n\n // Register the callback.\n measureClient.registerMeasureCallback(DataType.Companion.HEART_RATE_BPM, heartRateCallback)\n\n // Unregister the callback.\n awaitClose {\n runBlocking {\n measureClient.unregisterMeasureCallbackAsync(DataType.Companion.HEART_RATE_BPM, heartRateCallback)\n }\n }\n\n| **Note:** Kotlin developers can use `callbackFlow` to take advantage of coroutines and lifecycle. See the [Measure Data sample](https://github.com/android/health-samples/tree/main/health-services/MeasureDataCompose) on GitHub for an example."]]