这可能是一个新手问题,但我已经失去了一天的时间来弄清楚没有成功。我在我的 Android 应用程序中使用 Google Fit API,我需要显示它的一些数据,比如跑步和步行的日常步数。我设法以时间单位显示数据(fe 以 x 分钟运行)。我需要以步骤为单位显示它。
下面的代码片段显示了我如何以时间单位(以毫秒为单位)检索数据:
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(start, end, TimeUnit.MILLISECONDS)
.build();
Fitness.HistoryApi.readData(client, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
@Override
public void onResult(DataReadResult dataReadResult) {
if (dataReadResult.getBuckets().size() > 0) {
display.show("Bucket DataSet.size(): "
+ dataReadResult.getBuckets().size());
retrieveHistoryData(dataReadResult, client.getContext());
}
}
.....
private void retrieveHistoryData(DataReadResult dataReadResult, Context context) {
int walking = 0;
int running = 0;
int biking = 0;
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
display.show("dataSet.dataType: " + dataSet.getDataType().getName());
for (DataPoint dp : dataSet.getDataPoints()) {
walking += getBucketData(dp, FitnessActivities.WALKING);
running += getBucketData(dp, FitnessActivities.RUNNING);
biking += getBucketData(dp, FitnessActivities.BIKING);
}
}
}
Log.d(TAG, Constants.KEY_WALKING_STEPS + ": " + walking);
Log.d(TAG, Constants.KEY_RUNNING_STEPS + ": " + running);
Log.d(TAG, Constants.KEY_BIKING_STEPS + ": " + biking);
}
private int getBucketData(DataPoint dp, String activityName) {
int count = 0;
for (Field field : dp.getDataType().getFields()) {
Log.d(TAG, "Field " + field.getName() + " " + FitnessActivities.getName(dp.getValue(field).asInt()) + " = " + dp.getValue(field).asInt() + " field.describeContents()" + field.describeContents());
if ("activity".equals(field.getName()) && activityName.contentEquals(FitnessActivities.getName(dp.getValue(field).asInt()))) {
count += dp.getValue(field).asInt();
}
}
return count;
}
提前Tnx。
此致
MB