读取每日步数总计
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本部分介绍如何使用 Google 健身读取当前每日步数数据
Android API 和 Fit REST API。
Android
您的应用程序可以通过调用 读取当前的每日总步数。
HistoryClient.readDailyTotal
、
如以下示例中所示:
Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener { result ->
val totalSteps =
result.dataPoints.firstOrNull()?.getValue(Field.FIELD_STEPS)?.asInt() ?: 0
// Do something with totalSteps
}
.addOnFailureListener { e ->
Log.i(TAG, "There was a problem getting steps.", e)
}
每日总值是从当天的午夜开始计算,
当前时区。
要获取与 Google 健身应用相同的每日步数,请使用
com.google.android.gms
应用软件包,如以下示例所示:
val startTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault())
val endTime = LocalDateTime.now().atZone(ZoneId.systemDefault())
val datasource = DataSource.Builder()
.setAppPackageName("com.google.android.gms")
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_steps")
.build()
val request = DataReadRequest.Builder()
.aggregate(datasource)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS)
.build()
Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
.readData(request)
.addOnSuccessListener { response ->
val totalSteps = response.buckets
.flatMap { it.dataSets }
.flatMap { it.dataPoints }
.sumBy { it.getValue(Field.FIELD_STEPS).asInt() }
Log.i(TAG, "Total steps: $totalSteps")
}
如需详细了解如何使用汇总数据源,请参阅
使用健身历史记录。
REST
您的应用可以从所有数据中读取当前每日总步数
方法是发出 POST
请求并查询
指定时间段的“com.google.step_count.delta
”数据类型。
HTTP 方法
POST
Request URL
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
请求正文
{
"aggregateBy": [{
"dataTypeName": "com.google.step_count.delta",
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
}],
"bucketByTime": { "durationMillis": 86400000 },
"startTimeMillis": 1438705622000,
"endTimeMillis": 1439310422000
}
Curl 命令
curl \
-X POST \
-H "Content-Type: application/json;encoding=utf-8" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d @aggregate.json \
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis documentation demonstrates how to access current daily step count data using the Google Fit Android API and the Fit REST API.\u003c/p\u003e\n"],["\u003cp\u003eFor Android, you can retrieve the step count by using \u003ccode\u003eHistoryClient.readDailyTotal\u003c/code\u003e for a simple daily total or by creating a data source for a more detailed, Fit app-like count.\u003c/p\u003e\n"],["\u003cp\u003eTo match the step count displayed in the Google Fit app, ensure you specify the 'com.google.android.gms' app package when building your data source.\u003c/p\u003e\n"],["\u003cp\u003eThe REST API allows access to the daily step count across all data sources by sending a POST request to a specific endpoint with details about the data type and time period.\u003c/p\u003e\n"]]],[],null,["# Read the Daily Step Total\n\nThis section demonstrates reading current daily step count data using the Fit\nAndroid API and Fit REST API. \n\n### Android\n\nYour app can read the current daily step total by calling\n[`HistoryClient.readDailyTotal`](/fit/scenarios/android/reference/com/google/android/gms/fitness/HistoryClient#public-taskdataset-readdailytotal-datatype-datatype),\nas shown in the following example: \n\n```kotlin\nFitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)\n .addOnSuccessListener { result -\u003e\n val totalSteps =\n result.dataPoints.firstOrNull()?.getValue(Field.FIELD_STEPS)?.asInt() ?: 0\n // Do something with totalSteps\n }\n .addOnFailureListener { e -\u003e\n Log.i(TAG, \"There was a problem getting steps.\", e)\n }\n```\n\nThe daily total is computed from midnight of the current day on the device's\ncurrent timezone.\n\nTo get the same daily step count as the Fit app, create a data source using\nthe `com.google.android.gms`app package, as shown in the following example: \n\n```kotlin\nval startTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault())\nval endTime = LocalDateTime.now().atZone(ZoneId.systemDefault())\n\nval datasource = DataSource.Builder()\n .setAppPackageName(\"com.google.android.gms\")\n .setDataType(DataType.TYPE_STEP_COUNT_DELTA)\n .setType(DataSource.TYPE_DERIVED)\n .setStreamName(\"estimated_steps\")\n .build()\n\nval request = DataReadRequest.Builder()\n .aggregate(datasource)\n .bucketByTime(1, TimeUnit.DAYS)\n .setTimeRange(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS)\n .build()\n\nFitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n .readData(request)\n .addOnSuccessListener { response -\u003e\n val totalSteps = response.buckets\n .flatMap { it.dataSets }\n .flatMap { it.dataPoints }\n .sumBy { it.getValue(Field.FIELD_STEPS).asInt() }\n Log.i(TAG, \"Total steps: $totalSteps\")\n }\n```\n\nFor more information about working with aggregate data sources, see\n[Work with the Fitness History](/fit/android/history).\n\n### REST\n\nYour app can read the current daily step count total across all data\nsources by making a `POST` request and querying the\n`com.google.step_count.delta` data type for the specified time period.\n\n**HTTP method**\n\nPOST\n\n**Request URL** \n\n https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate\n\n**Request body** \n\n {\n \"aggregateBy\": [{\n \"dataTypeName\": \"com.google.step_count.delta\",\n \"dataSourceId\": \"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps\"\n }],\n \"bucketByTime\": { \"durationMillis\": 86400000 },\n \"startTimeMillis\": 1438705622000,\n \"endTimeMillis\": 1439310422000\n }\n\n**Curl command** \n\n curl \\\n -X POST \\\n -H \"Content-Type: application/json;encoding=utf-8\" \\\n -H \"Authorization: Bearer $ACCESS_TOKEN\" \\\n -d @aggregate.json \\\n https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate"]]