Wear OS 上的健康服务
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Wear OS 3 及更高版本包含一项名为健康服务的服务。健康服务充当设备上各种传感器和相关算法的中间层,为应用提供与活动、锻炼和健康相关的高质量数据。
如需查看健身应用示例,请参阅 GitHub 上的健康服务示例代码库。
健康服务如何帮助应用开发者
如果没有健康服务,应用必须连接到一个或多个传感器,适当配置每个传感器,接收原始传感器数据,并使用自己的算法来获取有意义的信息。例如,应用可能会注册 Sensor.TYPE_PRESSURE
的更新以获取当前气压,并使用它来计算当前海拔,并持续汇总这些数据以显示用户活动时段的高度变化。
健康服务会自动根据使用情形来配置所有与健康和健身相关的传感器,收集传感器数据,并计算心率、距离、卡路里、海拔、楼层、速度和步速等指标。应用可以直接从健康服务注册这些数据。

使用健康服务的优势包括:
- 利用在该平台上以原生方式运行的强大算法。
- 使用健康服务中针对节能进行了优化的传感器配置来节省电量。
- 为 Wear 3 及更高版本的设备提供面向未来的应用。Health Services API 在 Wear 3 及更高版本的设备上保持一致,可让您更轻松地更新应用。
- 通过使用标准化平台计算,可以确保同一设备上所有应用的数据保持一致。
- 启用活动感知体验,包括检测其他应用中启动并正在进行的锻炼。
凭借上述优势,开发者能够专注于开发独特的功能和用户体验,同时依靠平台以高能效的方式提供强大且一致的指标。
健康服务概念
使用健康服务进行开发时,请牢记以下概念。
数据类型
健康服务提供从设备上的所有可用来源收集并持续更新的各种数据。数据分为两大类别:在单一时间点采样的数据(如 HEART_RATE_BPM
)和在特定时间段内获取的数据(如 DISTANCE
)。
如需了解详情,请参阅数据类型的完整列表。
活动
当用户达到特定目标状态或事件时,应用会收到事件。例如,用户可以在锻炼应用中注册距离目标,那么当用户跑步达到一定距离时,应用会通知用户。或者,在用户达到特定步数或入睡的情况下使用被动目标。
如需了解详情,请参阅锻炼目标和被动目标。
锻炼类型
健康服务将锻炼视为一级功能,并支持多种锻炼目标,例如跑步或滑雪。在锻炼进行期间,健康服务可以收集所选数据类型的指标,并将报告发回给管理该锻炼的应用。
如需了解详情,请参阅锻炼类型的完整列表。
使用健康服务创建应用
您可按照下面几个部分所述的内容使用健康服务创建应用。
被动体验
借助 PassiveMonitoringClient
,您的应用可以实现 PassiveListenerService
,用于接收与数据类型或事件相关的更新。这适用于持续长时间且期间数据更新相对不频繁的体验。
如需了解详情,请参阅被动数据更新。
主动体验
借助 MeasureClient
,您的应用可以注册监听器以接收快速数据更新。这适用于短期体验,例如在用户查看应用界面时。尝试尽可能减少应用在注册监听器上所花费的时间,因为这会增加传感器采样率,进而增加功耗。此 API 不适用于后台捕获或锻炼跟踪。
如需了解详情,请参阅使用 MeasureClient 测量运动时的健康状况。
借助 ExerciseClient
,应用可以管理用户的锻炼活动、设置锻炼目标,并监听与当前锻炼状态有关的更新信息。另外,只要锻炼项目属于您的应用,您的应用就可以通过此 API 接收快速数据更新。ExerciseClient
可用于跑步应用之类的应用,此类应用允许用户记录跑步状况、在设备上显示实时指标以及记录数据以进行进一步分析。
如需了解详情,请参阅使用 ExerciseClient 记录运动。
使用合成数据进行测试
如需测试应用是否从健康服务接收数据更新,请手动设置设备以发出数据(模拟用户正在锻炼)。
如需了解详情,请参阅结合使用合成数据与健康服务。
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Health Services on Wear OS\n\nWear OS 3 and higher includes a service called\n[Health Services](/jetpack/androidx/releases/health).\nHealth Services acts as an intermediary to the various sensors and related\nalgorithms on the device to provide apps with high-quality data related to\nactivity, exercise, and health.\n\nSee the\n[Health Services samples repository](https://github.com/android/health-samples/tree/main/health-services)\non GitHub for example fitness apps.\n\nHow Health Services helps app developers\n----------------------------------------\n\nWithout Health Services, apps must connect to one or multiple sensors,\nconfigure each of them appropriately, receive raw sensor data, and use their\nown algorithms to derive meaningful information. For example, an app might\nregister for updates of\n[`Sensor.TYPE_PRESSURE`](/reference/kotlin/android/hardware/Sensor#type_pressure)\nto get the current air pressure, use it to compute the current altitude, and\naggregate this data over time to show the elevation changes during a user's\nactivity session.\n\nHealth Services automatically configures all fitness and health\nrelated sensors appropriately for the use-case, collects sensor data, and computes\nmetrics like heart rate, distance, calories, elevation, floors, speed, pace,\nand more. Apps can register for this data directly from Health Services.\n\nSome of the benefits of using Health Services include the following:\n\n- Takes advantage of powerful algorithms running natively on the platform.\n- Conserves battery by using sensor configurations from Health Services that are optimized for power efficiency.\n- Future-proofs an app for Wear 3+ devices. The Health Services API is consistent across Wear 3+ devices, making it easier to keep your app up to date.\n- Ensures data consistency across all applications on the same device by using standardized platform computations.\n- Enables activity-aware experiences, including the ability to detect an ongoing exercise started from another app.\n\nThese benefits let developers focus on developing unique features and user\nexperiences, while relying on the platform to provide robust and consistent\nmetrics in a power-efficient manner.\n\nHealth Services concepts\n------------------------\n\nKeep the following concepts in mind when developing with Health Services.\n\n### Data types\n\nHealth Services offers a variety of data collected and continuously updated\nfrom all the available sources on the device. The data falls into two\nbroad categories: data sampled at a single point in time, such as\n`HEART_RATE_BPM`, and data taken over a time interval, such as `DISTANCE`.\n\nFor more information, see the full list of [Data types](/reference/kotlin/androidx/health/services/client/data/DataType).\n\n### Events\n\nApps receive events when the user reaches a certain goal state or event. For\nexample, a user can register a distance goal within an exercise app, and then\nthe app notifies the user when they have run a certain distance.\nAlternatively, use a passive goal for situations like the user hitting a\ncertain step count or falling asleep.\n\nFor more information, see [Exercise goals](/reference/kotlin/androidx/health/services/client/data/ExerciseGoal) and [Passive goals](/reference/kotlin/androidx/health/services/client/data/PassiveGoal).\n\n### Exercise types\n\nHealth Services treats exercise as a first-class feature and supports a\nmultitude of exercise types, such as running or skiing. While an exercise is in\nprogress, Health Services can collect metrics on selected data types and report\nback to the app that manages the exercise.\n\nFor more information, see the full list of [exercise types](/reference/kotlin/androidx/health/services/client/data/ExerciseType).\n\nCreate apps using Health Services\n---------------------------------\n\nCreate apps using Health Services as described in the following\nsections.\n\n### Passive experiences\n\nUsing\n[`PassiveMonitoringClient`](/reference/kotlin/androidx/health/services/client/PassiveMonitoringClient),\nyour app implements a `PassiveListenerService` that receives updates about a\ndata type or an event. This is suited for long-lived experiences where data\nupdates are relatively infrequent.\n\nSee [Passive data updates](/training/wearables/health-services/passive)\nfor more information.\n\n### Active experiences\n\nUsing `MeasureClient`, your app registers listeners to receive rapid data\nupdates. This is suited for short-lived experiences, such as while the user\nlooks at your app UI. Try to minimize the time your app\nspends with a registered listener, because it increases the sensor sampling rate\nand thus increases power consumption. This API is not intended for background\ncapture or workout tracking.\n\nSee [Take spot health measurements with MeasureClient](/training/wearables/health-services/active-data/measure-client)\nfor more information.\n\nUsing `ExerciseClient`, your app can manage a user's workout, set exercise goals,\nand listen for updates about the current exercise state. Your app can also\nreceive rapid data updates through this API, as long as the exercise belongs to\nyour app. `ExerciseClient` can be used for apps such as a running app that\nlets users record their run, displays live metrics on their device, and records\ndata for further analysis.\n\nSee [Record an exercise with ExerciseClient](/training/wearables/health-services/active-data)\nfor more information.\n\n### Test with synthetic data\n\nTo test that your app is receiving data updates from Health Services, manually\nset your device to emit data as if a user were engaged in an exercise.\n\nSee [Use synthetic data with Health\nServices](/training/wearables/health-services/synthetic-data) for more information.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Record an exercise with ExerciseClient](/training/wearables/health-services/active-data)\n- [Take spot health measurements with MeasureClient](/training/wearables/health-services/active-data/measure-client)\n- [Passive data updates](/training/wearables/health-services/passive)\n- [Enhance app compatibility across Wear OS devices](/training/wearables/health-services/compatibility)"]]