头图

By Chris Wilk, Product Manager

From the MyFitnessPal app that helps you keep track of your three meals a day, to the Withings products that provide you with a comprehensive analysis of your fitness level, apps and devices have formed a data source that aggregates multiple health and fitness information. If you're an Android developer, connecting and sharing this data between apps can help you better understand your users and provide them with a richer experience. However, most of this health data is scattered on multiple devices with different experiences, so it has been difficult to integrate in the past, and there is no unified privacy management scheme to protect Android users' data.

Learn about Health Connect

To address the issues mentioned at the beginning of this article, we have introduced Health Connect , which is both a platform and an API for Android app developers. With the user's permission, developers can use this set of APIs to access and share users' health and fitness data across Android devices.

We are currently working with Samsung to build this new unified platform with the goal of simplifying the connection between applications. Together with Samsung, we launched Health Connect to create a richer app experience and support unified privacy controls for users.

In the meantime, we've been running early access programs with developers from MyFitnessPal, Leap Fitness, and Withings. Additionally, Samsung Health, Google Fit and Fitbit are integrating into Health Connect. The Android API set provided by Health Connect is available to all developers through Android Jetpack.

Health Connect connects and empowers health information by using its own platform and technology, which aligns with Google Health's broad vision of "Helping billions of people be healthier" .

How does Health Connect work?

△ Health Connect 的运作方式

△ How Health Connect works

Health Connect supports many common types and categories of health and fitness data, including: activity, sleep quality, nutritional intake, physical measurements, and important physical information such as heart rate and blood pressure.

With user consent, developers can securely read or write data from Health Connect through standardized schemas and API calls. Users have complete control over their privacy settings, and Health Connect provides them with fine-grained visibility into which apps are requesting data access at a certain point in time. All data generated by Health Connect is encrypted and stored locally on the device. Users can completely disable access to Health Connect data, or delete some data that they do not want to store in the device. Health Connect also gives users the option to prioritize a data source when using multiple apps of the same type.

start using

Connecting your app to Health Connect is easy. This set of APIs for Health Connect simplifies permissions management and data reading and writing. The sample code below shows you how to request permission and then write some data.

First, construct a set of authorization permissions that you wish to request to read or write. In this example, we will read and write steps taken and heart rate data.

 private val permissions =
  setOf(
    Permission.createReadPermission(Steps::class),
    Permission.createWritePermission(Steps::class),
    Permission.createReadPermission(HeartRate::class),
    Permission.createWritePermission(HeartRate::class),
  )

// 随后,为这组权限创建一个权限请求。您可以使用 ActivityCompat.requestPermissions() 方法。

Then, the permission request just created is initiated, and the user can grant the relevant permission on the authorization interface of Health Connect.

Once the user is authorized, you can read and write data. The following code snippet shows how to write step information over a period of time. First, this set of data includes the total number of steps, start and end times, and time zone information for the period. This set of data is then inserted into Health Connect.

 private suspend fun writeSomeData(client: HealthConnectClient) {
    val records = mutableListOf<Record>()

    records.add(
      Steps(
        count = 888,
        startTime = START_TIME,
        endTime = END_TIME,
        startZoneOffset = null,
        endZoneOffset = null,
      )
    )
    // 如果有需要的话,可以增加额外的记录项
}

understand more

Health Connect is now open to developers, see the following resources:

To learn more about it, you can watch the presentation video .

You are welcome to click here to submit feedback to us, or to share what you like and problems you find. Your feedback is very important to us, thank you for your support!


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。