3

这可能是一个新手问题,但我已经失去了一天的时间来弄清楚没有成功。我在我的 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

4

1 回答 1

1

您可以使用以下代码获取每天各种活动的步骤

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

您可以循环运行此代码以获取任意天数的数据。它将返回所有涉及步骤的活动的数据桶,您可以通过此方法从桶中获取活动名称,该方法将活动名称作为字符串返回

bucket.getActivity();

希望能帮助到你!:)

于 2016-09-23T08:51:34.220 回答