如何从Google Fit API检索每日跑步和步行

时间:2015-06-13 10:55:24

标签: android google-fit google-fit-sdk

这可能是一个新手问题,但我已经失去了一天,没有成功。 我在我的Android应用中使用Google Fit API,我需要显示一些数据,例如跑步和走路每日步骤。我已设法以时间单位显示数据(f.e以x min运行)。我需要以步骤单位显示它。

下面的剪切代码显示了我如何以时间单位(以毫秒为单位)检索数据:

  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

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码

获取每天各种活动的步骤
DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByActivityType(1, TimeUnit.SECONDS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

其中ESTIMATED_STEP_DELTAS定义为

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();

您可以循环运行此代码,以便根据需要获取数天。它将为所有涉及步骤的活动返回数据桶,您可以通过此方法从存储桶中获取活动名称,该方法将活动名称返回为String

bucket.getActivity();

希望它有所帮助! :)