Google Fit查询手动启动的活动

时间:2018-08-19 10:13:01

标签: java android google-fit

我正在尝试查询由用户开始进入google fit的google fit活动,按+-> start activity-> start,而不是通常被动跟踪的活动,我本来以为这是会话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();
}

当时我希望从历史上获取活动数据,但是当我没有从查询中获得任何结果时,我以为我使用的是错误的api。

1 个答案:

答案 0 :(得分:0)

好吧,所以最终我弄清楚了我可以使用历史记录api实现我想要的东西,关键是通过会话进行BUCKET,所以首先创建这样的请求:

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!!
        }
     });
 }