Google 适合查询手动启动的活动

Google Fit query manually started activites

我正在尝试查询 google 由用户开始的适合活动进入 google 适合按 + -> 开始 activity -> 开始,而不是通常被动跟踪 activity,我最初认为这就是会议 api 的目的,但我认为这可能是为了其他目的。

private SessionReadRequest querySessionData() {
    // Set a start and end time for our query, using a start time of 4 week before this moment.
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.WEEK_OF_YEAR, -4);
    long startTime = cal.getTimeInMillis();

    // Build a session read request
    return new SessionReadRequest.Builder()
            .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
            .read(DataType.TYPE_SPEED)
            //.setSessionName()
            .build();
}

当时我希望获得 activity 历史数据,但是当我没有从查询返回结果时,我认为我使用了错误的 api.

好吧,最终我发现我可以使用历史 api 实现我想要的,关键是 BUCKET BY SESSION,所以首先像这样创建请求:

private DataReadRequest querySessionData() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    //query 30 days in the past
    cal.add(Calendar.DAY_OF_MONTH, - 30);
    long startTime = cal.getTimeInMillis();


    return new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
            .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
            .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
            .aggregate(DataType.TYPE_SPEED, DataType.AGGREGATE_SPEED_SUMMARY)
            .aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY)

            .bucketBySession(1, TimeUnit.MINUTES)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();
}

然后要使用该请求,请像这样使用历史记录 api:

final DataReadRequest readRequest = querySessionData();
Fitness.HistoryApi.readData(mClient, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
        @Override
        public void onResult(@NonNull DataReadResult dataReadResult) {
            //do your stuff!!
        }
     });
 }