如何使用readDailyTotal从Google Fit History API中读取循环和运行数据

时间:2016-04-16 19:22:26

标签: android google-fit

我正在尝试使用方法readDailyTotal读取Fitness History Api中的每日数据。我想阅读步骤,骑自行车和跑步。我已经成功实施了步骤,但我无法获得骑行和跑步的每日结果。

这是我用来获取步骤的代码,它起作用:

PendingResult<DailyTotalResult> resultSteps
                = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA);

resultSteps.setResultCallback(new ResultCallback<DailyTotalResult>() {
      @Override
      public void onResult(@NonNull DailyTotalResult dailyTotalResult) {
          if (dailyTotalResult.getStatus().isSuccess()) {
            DataSet dataSet = dailyTotalResult.getTotal();

             for (DataPoint dp : dataSet.getDataPoints()) {
                Log.i(TAG, "DataType: " + dp.getDataType().getName());
                for (Field field : dp.getDataType().getFields()) {
                    Log.i(TAG, "\tField: " + field.getName() +
                            " Value: " + dp.getValue(field));
                    Log.i(TAG, "Field: " + field.getName());
                    Log.i(TAG, "Field: " + field.getFormat());
                    Log.i(TAG, "Field: " + dp.getDataType().getName());
                    Log.i(TAG, "---------------------------------");
                }
              }
          } 
      }
});

我尝试使用相同的代码但是骑自行车:

PendingResult<DailyTotalResult> resultCycling
                    = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient, DataType.TYPE_CYCLING_WHEEL_RPM);

我也尝试使用不同的DataType,如: TYPE_CYCLING_PEDALING_CADENCETYPE_CYCLING_PEDALING_CUMULATIVETYPE_CYCLING_WHEEL_REVOLUTION

它不起作用。我也阅读了整个Fitness Fit文档,但我没有得到如何做到这一点。

所以,我的问题是如何获取骑行和跑步的每日数据?如果你能给我一些片段,我将不胜感激。

谢谢:)

1 个答案:

答案 0 :(得分:0)

我不确定自行车踩踏数据,但您可以获得这样的自行车类型数据:

      DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi.readData(client, readRequest);

    DataReadResult dataReadResult = pendingResult.await(60, TimeUnit.SECONDS);
    Log.d(TAG, "Retrieved results: " + dataReadResult);
    if (dataReadResult.getStatus().isSuccess()) {
        if (dataReadResult.getBuckets().size() > 0) {

            for (Bucket bucket : dataReadResult.getBuckets()) {

                List<DataSet> dataSets = bucket.getDataSets();
                Log.d(TAG, "getFitHistoryByTime: Activity " + bucket.getActivity());
                Log.d(TAG, "getFitHistoryByTime: Start - End  "
                        + dateFormat.format(bucket.getStartTime(TimeUnit.MILLISECONDS))
                        + " - "
                        + dateFormat.format(bucket.getEndTime(TimeUnit.MILLISECONDS)));
                for (DataSet dataSet : dataSets) {
                    Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
                    for (DataPoint dp : dataSet.getDataPoints()) {

                         if (dp.getDataType().equals(DataType.AGGREGATE_ACTIVITY_SUMMARY)) {
                            boolean activityToAdd = false;
                            ActivitiesData activitiesData = new ActivitiesData();
                            for (Field field : dp.getDataType().getFields()) {
                                if (Field.FIELD_ACTIVITY.equals(field)) {
                                    int activityType = dp.getValue(field).asInt();
                                    Log.d(TAG, "getFitHistoryByTime: activity type " + activityType);
                                    if (FitnessActivities.getName(activityType).equals("biking")) {
                                        activityToAdd = true;
                                        Log.d(TAG, "getFitHistoryByTime: added activity: " + FitnessActivities.getName(activityType));

                                    }
                                }

                                if (Field.FIELD_DURATION.equals(field)) {
                                    long duration = dp.getValue(field).asInt();  
                                }
                            }
                        }
                    }
                }


            .....

也许你会根据这段代码获得想法。 另外,请尝试在Google健身G+ community上发布此问题。

亲切的问候。 MB