2

所以我正在尝试使用 android API 阅读 google fit 历史记录,但我遇到了一个恼人的限制。我似乎只能阅读过去一周左右的历史价值,即使有更多的数据(根据 google fit 应用程序)。我用来读取数据的代码如下

    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.MONTH, -1);
    long startTime = cal.getTimeInMillis();
    //long startTime = 0l;

    final SimpleDateFormat dateFormat = new SimpleDateFormat();
    Log.i(TAG, "Range start: " + dateFormat.format(startTime));
    Log.i(TAG, "Range end: " + dateFormat.format(endTime));

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

    Fitness.HistoryApi.readData(client, readRequest).setResultCallback(
            new com.google.android.gms.common.api.ResultCallback<DataReadResult>() {

                @Override
                public void onResult(DataReadResult result) {
                    //Log.i(TAG, result.getStatus().getStatusMessage());
                    int i = 0;
                    for (Bucket bucket : result.getBuckets()) {
                        for (DataSet dataSet : bucket.getDataSets()) {
                            //Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
                            for (DataPoint dp : dataSet.getDataPoints()) {
                                Log.i(TAG, ++i + "");
                                Log.i(TAG, "Data point:");
                                Log.i(TAG, "\tType: " + dp.getDataType().getName());
                                Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                                Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
                                for (Field field : dp.getDataType().getFields()) {
                                    Log.i(TAG, "\tField: " + field.getName() +
                                            " Value: " + dp.getValue(field));
                                }
                            }
                        }
                    }
                }
            }
    );

然而根据 logcat 的说法,有很多空闲的日子

0-01 22:24:21.000 32590-32624/? I/MainActivity: Range start: 2015-09-01 10:24 PM
10-01 22:24:21.000 32590-32624/? I/MainActivity: Range end: 2015-10-01 10:24 PM
10-01 22:24:21.380 32590-32606/? V/Fitness: Received batch result
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.380 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity: 1
10-01 22:24:21.383 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.383 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.383 32590-32590/? I/MainActivity:    Start: 2015-09-23 4:41 PM
10-01 22:24:21.384 32590-32590/? I/MainActivity:    End: 2015-09-23 10:14 PM
10-01 22:24:21.384 32590-32590/? I/MainActivity:    Field: steps Value: 3093
10-01 22:24:21.384 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.384 32590-32590/? I/MainActivity: 2
10-01 22:24:21.384 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.384 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.385 32590-32590/? I/MainActivity:    Start: 2015-09-23 10:27 PM
10-01 22:24:21.385 32590-32590/? I/MainActivity:    End: 2015-09-24 5:14 PM
10-01 22:24:21.385 32590-32590/? I/MainActivity:    Field: steps Value: 13582
10-01 22:24:21.385 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.385 32590-32590/? I/MainActivity: 3
10-01 22:24:21.385 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.385 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.385 32590-32590/? I/MainActivity:    Start: 2015-09-25 11:54 AM
10-01 22:24:21.385 32590-32590/? I/MainActivity:    End: 2015-09-25 10:23 PM
10-01 22:24:21.385 32590-32590/? I/MainActivity:    Field: steps Value: 15395
10-01 22:24:21.385 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.385 32590-32590/? I/MainActivity: 4
10-01 22:24:21.385 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.385 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.386 32590-32590/? I/MainActivity:    Start: 2015-09-25 10:34 PM
10-01 22:24:21.386 32590-32590/? I/MainActivity:    End: 2015-09-26 10:23 PM
10-01 22:24:21.386 32590-32590/? I/MainActivity:    Field: steps Value: 15975
10-01 22:24:21.386 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.386 32590-32590/? I/MainActivity: 5
10-01 22:24:21.386 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.386 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.386 32590-32590/? I/MainActivity:    Start: 2015-09-26 10:25 PM
10-01 22:24:21.386 32590-32590/? I/MainActivity:    End: 2015-09-27 10:24 PM
10-01 22:24:21.386 32590-32590/? I/MainActivity:    Field: steps Value: 14562
10-01 22:24:21.386 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.386 32590-32590/? I/MainActivity: 6
10-01 22:24:21.386 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.386 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.387 32590-32590/? I/MainActivity:    Start: 2015-09-27 10:24 PM
10-01 22:24:21.387 32590-32590/? I/MainActivity:    End: 2015-09-28 10:24 PM
10-01 22:24:21.387 32590-32590/? I/MainActivity:    Field: steps Value: 16200
10-01 22:24:21.387 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.387 32590-32590/? I/MainActivity: 7
10-01 22:24:21.387 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.387 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.387 32590-32590/? I/MainActivity:    Start: 2015-09-28 10:24 PM
10-01 22:24:21.387 32590-32590/? I/MainActivity:    End: 2015-09-29 10:21 PM
10-01 22:24:21.387 32590-32590/? I/MainActivity:    Field: steps Value: 13371
10-01 22:24:21.387 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.387 32590-32590/? I/MainActivity: 8
10-01 22:24:21.387 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.387 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.388 32590-32590/? I/MainActivity:    Start: 2015-09-29 10:37 PM
10-01 22:24:21.388 32590-32590/? I/MainActivity:    End: 2015-09-30 10:19 PM
10-01 22:24:21.388 32590-32590/? I/MainActivity:    Field: steps Value: 3383
10-01 22:24:21.388 32590-32590/? I/MainActivity: Data returned for Data type: com.google.step_count.delta
10-01 22:24:21.388 32590-32590/? I/MainActivity: 9
10-01 22:24:21.388 32590-32590/? I/MainActivity: Data point:
10-01 22:24:21.388 32590-32590/? I/MainActivity:    Type: com.google.step_count.delta
10-01 22:24:21.388 32590-32590/? I/MainActivity:    Start: 2015-09-30 10:45 PM
10-01 22:24:21.388 32590-32590/? I/MainActivity:    End: 2015-10-01 7:26 PM
10-01 22:24:21.389 32590-32590/? I/MainActivity:    Field: steps Value: 12805

任何关于我为什么不能在 23 日之前阅读的见解(截至写作时)将不胜感激。

4

1 回答 1

4

默认情况下,您的请求将仅查询存储在设备上的本地数据。要包含来自 Google 服务器的数据,请添加.enableServerQueries()DataReadRequest.Builder

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

启用服务器查询()

启用查询 Google Fit 服务器以获取查询结果,以防本地商店没有完整请求时间范围内的数据。服务器结果将与本地结果合并到一个 DataSet 中。

于 2016-05-30T17:58:37.283 回答