从 Google Fit Android API 检索最近的步行

Retrieve recent walks from the Google Fit Android API

目前我正在尝试开发一个本机 Android 应用程序来从 History_API 检索信息。我已经能够检索卡路里、每日步数和活动时间。现在我正在考虑将其提升到一个新的水平,并尝试使用以下代码查看从历史记录 api 中检索到的不同类型的活动:

    protected Void doInBackground(Void... params) {
        DataReadRequest readRequest = queryFitnessData();

        DataReadResult dataReadResult =
                Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1, TimeUnit.MINUTES);

        getData(dataReadResult);
        return null;
   }

这是定义请求的函数:

    private DataReadRequest queryFitnessData() {
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.WEEK_OF_MONTH, -1);
        long startTime = cal.getTimeInMillis();

        DataSource ACTIVITY_SEGMENT = new DataSource.Builder()
                .setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
                .setType(DataSource.TYPE_DERIVED)
                .build();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
                .bucketByTime(5, TimeUnit.MINUTES)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        return readRequest;
    }

这在下午似乎工作正常,但在刚才进一步处理时,它不再显示视图。查看日志文件表明,存储桶返回了 'in_vehicle' 类型的所有位置。这就是为什么下面的代码没有将视图添加到我的数组中的原因。

    private void getData(DataReadResult result) {
        if (result.getBuckets().size() > 0) {
            Log.i(TAG, "Bucket size: " + result.getBuckets().size());
            for(Bucket bucket : result.getBuckets()) {
                long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES);

                Log.i(TAG, bucket.getActivity());

                switch (bucket.getActivity()) {
                    case "walking":
                        Log.i(TAG, "Adding a walking card");
                        activityData.add(new CustomActivityCard("Walking","Place, 27 juli",Long.toString(activeTime) + " minutes","5 kilometers","2340 steps","1340 calories"));
                        break;
                    case "running":
                        Log.i(TAG, "Adding a running card");
                        activityData.add(new CustomActivityCard("Running","Place, 27 juli",Long.toString(activeTime) + " minutes","5 kilometers","2340 steps","1340 calories"));
                        break;
                    case "biking":
                        Log.i(TAG, "Adding a cycling card");
                        activityData.add(new CustomActivityCard("Cycling","Place, 27 juli",Long.toString(activeTime) + " minutes","5 kilometers","2340 steps","1340 calories"));
                        break;
                }
            }
        }
    }

我不明白为什么今天下午代码显示步行和 运行 活动,而突然间只有 returns 我开车时的活动。我能想到的一种可能的解决方案是我的应用程序未订阅任何数据类型。所以我的问题是:

  1. 我的应用程序是否需要订阅才能检索活动(例如步行)?
  2. 我应该如何检索一天或一周内执行的所有活动?

我不确定评论中建议的订阅是否有任何不同,但为了获得解决方案肯定有帮助的一件事是通过更改下面的代码片段:

DataReadRequest readRequest = new DataReadRequest.Builder()
    .aggregate(ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
    .bucketByTime(5, TimeUnit.MINUTES)
    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    .build();

对于像下面的 DataRequest 这样的东西,这让我能够检索当天的步行活动

DataReadRequest readRequest = new DataReadRequest.Builder()
    .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
    .bucketByActivitySegment(7, TimeUnit.MINUTES)
    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    .build();

希望这对其他人也有帮助!