问题
Stepcount is different when accessed by getDailyTotal and getData bucketed by day. Please help.
Fitness.HistoryApi.readDailyTotal(App.gClient, DataType.TYPE_STEP_COUNT_DELTA)
-> returns step count as 1515
private void getStepsDataHistory(long startTime, long endTime) {
DataReadRequest dataReadRequest = new DataReadRequest.Builder()
.bucketByTime(1, TimeUnit.DAYS)
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();
-> returns data for a week, where today's steps =2877
EDIT: I also get this warning in the logcat: Couldn't find aggregated data in Shared Pref for DataType{com.google.step_count.delta{steps(i)}}.
回答1:
Please make sure that the start time and end time you gave are correct i.e from 12Am midnight to now. However readDailyTotal can be useful if you require step data for use in areas where you are unable to show the permissions panel (for example, Android Wear watch faces). So i advised you to use Aggregate method in that way you will get quiet equal step count as google fit app . Check this out .
public int getStepsCount(long startTime, long endTime) {
PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi
.readData(
fitnessClient,
new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA,
DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime,
TimeUnit.MILLISECONDS)
.build());
int steps = 0;
DataReadResult dataReadResult = pendingResult.await();
if (dataReadResult.getBuckets().size() > 0) {
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
for (DataPoint dp : dataSet.getDataPoints()) {
for (Field field : dp.getDataType().getFields()) {
steps += dp.getValue(field).asInt();
}
}
}
}
} else if (dataReadResult.getDataSets().size() > 0) {
for (DataSet dataSet : dataReadResult.getDataSets()) {
for (DataPoint dp : dataSet.getDataPoints()) {
for (Field field : dp.getDataType().getFields()) {
steps += dp.getValue(field).asInt();
}
}
}
}
return steps;
}
来源:https://stackoverflow.com/questions/35546979/google-fit-api-returns-different-step-counts