通过 Google Fit Android API 检索的每周步数与 Google Fit 官方应用程序中显示的步数不匹配
Weekly Steps Retrieved through Google Fit Android API doesn't match with steps displayed in Google Fit Official App
我正在尝试从 google fit API 中检索用户的每周步数数据,但我从官方 google fit 应用程序数据中得到不同的步数结果。例如:通过 google fit api 检索到的星期四步数是 5244 而在官方 google fit 应用程序中,星期四的步数是 5134。一周中每一天的步数都略有不同,但我想要准确的结果。我正在使用 google fit History API 来检索每周步数并显示在条形图中。
我的代码如下所示:
fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_READ)
.build();
@RequiresApi(api = Build.VERSION_CODES.O)
private void invokeHistoryApiForWeeklySteps() {
// Read the data that's been collected throughout the past week.
ZonedDateTime endTime = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
endTime = LocalDateTime.now().atZone(ZoneId.systemDefault());
}
ZonedDateTime startTime = endTime.minusWeeks(1);
Log.i(TAG, "Range Start: $startTime");
Log.i(TAG, "Range End: $endTime");
DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_steps")
.setAppPackageName("com.google.android.gms")
.build();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS)
.enableServerQueries()
.build();
Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
.readData(readRequest)
.addOnSuccessListener(response -> {
// The aggregate query puts datasets into buckets, so convert to a
// single list of datasets
for (Bucket bucket : response.getBuckets()) {
//convert days in bucket to milliseconds
long days = bucket.getStartTime(TimeUnit.MILLISECONDS);
//convert milliseconds to date
Date stepsDate = new Date(days);
//convert date to day of the week eg: monday, tuesday etc
@SuppressLint("SimpleDateFormat")
DateFormat df = new SimpleDateFormat("EEE");
String weekday = df.format(stepsDate);
Log.i(TAG, stepsDate.toString());
theDates.add(weekday);
for (DataSet dataSet : bucket.getDataSets()) {
totalAvgSteps.add(dumpDataSet(dataSet));
}
}
Log.i(TAG, theDates.toString());
Log.i(TAG, totalAvgSteps.toString());
displayBarChart();
})
.addOnFailureListener(e ->
Log.w(TAG, "There was an error reading data from Google Fit", e));
}
private int dumpDataSet(DataSet dataSet) {
Log.i(TAG, "Data returned for Data type: ${dataSet.dataType.name}");
int totalSteps = 0;
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(TAG, "Data point:");
Log.i(TAG, "\tType: ${dp.dataType.name}");
Log.i(TAG, "\tStart: ${dp.getStartTimeString()}");
Log.i(TAG, "\tEnd: ${dp.getEndTimeString()}");
for (Field field : dp.getDataType().getFields()) {
String fieldName = field.getName();
totalSteps += dp.getValue(field).asInt();
Log.i(TAG, "\tfield: " + fieldName + "value: " + dp.getValue(field));
}
}
return totalSteps;
}
这是我通过这段代码得到的结果:
我的代码结果:
这就是 google Fit 官方应用中的数据
步数计入 Google Fit 应用程序:
Google 健身应用中的步数:
所以我解决了步数差异的问题,如果它也能帮助到其他人,那么在这里分享。
我犯的错误是我没有计算从昨天午夜 12:00 到上周午夜 12:00 的数据,这导致每天的步数不同。
这是正确的日期和时间计算:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_MONTH, -1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
Log.i(TAG, String.valueOf(endTime));
Log.i(TAG,String.valueOf(startTime) );
并且我们需要稍微更改数据请求,我在几秒钟内请求它,现在我请求以毫秒为单位,这为我提供了 google 适合官方应用程序的确切数据。
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
其余代码与我在问题中发布的一样。
我正在尝试从 google fit API 中检索用户的每周步数数据,但我从官方 google fit 应用程序数据中得到不同的步数结果。例如:通过 google fit api 检索到的星期四步数是 5244 而在官方 google fit 应用程序中,星期四的步数是 5134。一周中每一天的步数都略有不同,但我想要准确的结果。我正在使用 google fit History API 来检索每周步数并显示在条形图中。 我的代码如下所示:
fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_READ)
.build();
@RequiresApi(api = Build.VERSION_CODES.O)
private void invokeHistoryApiForWeeklySteps() {
// Read the data that's been collected throughout the past week.
ZonedDateTime endTime = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
endTime = LocalDateTime.now().atZone(ZoneId.systemDefault());
}
ZonedDateTime startTime = endTime.minusWeeks(1);
Log.i(TAG, "Range Start: $startTime");
Log.i(TAG, "Range End: $endTime");
DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_steps")
.setAppPackageName("com.google.android.gms")
.build();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS)
.enableServerQueries()
.build();
Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
.readData(readRequest)
.addOnSuccessListener(response -> {
// The aggregate query puts datasets into buckets, so convert to a
// single list of datasets
for (Bucket bucket : response.getBuckets()) {
//convert days in bucket to milliseconds
long days = bucket.getStartTime(TimeUnit.MILLISECONDS);
//convert milliseconds to date
Date stepsDate = new Date(days);
//convert date to day of the week eg: monday, tuesday etc
@SuppressLint("SimpleDateFormat")
DateFormat df = new SimpleDateFormat("EEE");
String weekday = df.format(stepsDate);
Log.i(TAG, stepsDate.toString());
theDates.add(weekday);
for (DataSet dataSet : bucket.getDataSets()) {
totalAvgSteps.add(dumpDataSet(dataSet));
}
}
Log.i(TAG, theDates.toString());
Log.i(TAG, totalAvgSteps.toString());
displayBarChart();
})
.addOnFailureListener(e ->
Log.w(TAG, "There was an error reading data from Google Fit", e));
}
private int dumpDataSet(DataSet dataSet) {
Log.i(TAG, "Data returned for Data type: ${dataSet.dataType.name}");
int totalSteps = 0;
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(TAG, "Data point:");
Log.i(TAG, "\tType: ${dp.dataType.name}");
Log.i(TAG, "\tStart: ${dp.getStartTimeString()}");
Log.i(TAG, "\tEnd: ${dp.getEndTimeString()}");
for (Field field : dp.getDataType().getFields()) {
String fieldName = field.getName();
totalSteps += dp.getValue(field).asInt();
Log.i(TAG, "\tfield: " + fieldName + "value: " + dp.getValue(field));
}
}
return totalSteps;
}
这是我通过这段代码得到的结果:
我的代码结果:
这就是 google Fit 官方应用中的数据
步数计入 Google Fit 应用程序:
Google 健身应用中的步数:
所以我解决了步数差异的问题,如果它也能帮助到其他人,那么在这里分享。 我犯的错误是我没有计算从昨天午夜 12:00 到上周午夜 12:00 的数据,这导致每天的步数不同。 这是正确的日期和时间计算:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_MONTH, -1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
Log.i(TAG, String.valueOf(endTime));
Log.i(TAG,String.valueOf(startTime) );
并且我们需要稍微更改数据请求,我在几秒钟内请求它,现在我请求以毫秒为单位,这为我提供了 google 适合官方应用程序的确切数据。
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
其余代码与我在问题中发布的一样。