我需要获取我的拟合数据以反映android fit应用程序中显示的内容。我尝试使用传感器API,但这个数字反映了自设备启动以来所采取的步骤数量,而不是当天。
How to get step count from Google Fit REST API like Google Fit app?
我也尝试过使用History API并查询stepcount_cumulative和step_delta。这两个值都不反映fit API的值。
我想出了将前一天的值存储在sharedPref中并减去差值以获得真值的想法。设备重置的其他计算也需要考虑在内。
有没有更简单的方法来获取我需要的值?
答案 0 :(得分:1)
尝试这种新方法HistoryApi.readDailyTotal()
。 (需要Google Play服务v24)
PendingResult<DailyTotalResult> result = HistoryApi.readDailyTotal(client, TYPE_STEP_COUNT_DELTA);
DailyTotalResult totalResult = result.await(30, SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
long total = totalSet.isEmpty()
? 0
: totalSet.getDataPoints().get(0).getValue(FIELD_STEPS).asInt();
} else {
// handle failure
}
https://developer.android.com/reference/com/google/android/gms/fitness/HistoryApi.html
答案 1 :(得分:0)
这对我有用:https://github.com/rsteckler/Android-Google-Fit-Service-Skeleton
您正在寻找的功能:
private void getStepsToday() {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
final DataReadRequest readRequest = new DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
DataReadResult dataReadResult =
Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);
DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
int totalSteps = 0;
for (DataPoint dp : stepData.getDataPoints()) {
for(Field field : dp.getDataType().getFields()) {
int steps = dp.getValue(field).asInt();
totalSteps += steps;
}
}
publishTodaysStepData(totalSteps);
}