谷歌适合历史API

时间:2017-08-01 10:18:47

标签: java android google-api google-fit

我正在尝试使用Google Fit History API来获取有关我的帐户记录的步数,卡路里和距离的详细信息。所以我可以生成一个列表,其中包含该日期记录的天数和金额。然而,它似乎并没有真正回到过去,我似乎很难将这些细节放在列表中。下面的代码只显示我为卡路里所做的事情,但步数和距离遵循相同的原则。我也用OAUTH id注册了我的客户端。

private float showCalories(DataReadResult dataReadResult) {
    List<DailyCalories> mCaloriesList = new ArrayList<>();
    float calories = 0;
    DateFormat dateFormat = DateFormat.getDateInstance();
    Log.i(TAG, "Range Start: " + dateFormat.format(startWeek));
    Log.i(TAG, "Range End: " + dateFormat.format(endWeek));

    Bucket bucket = dataReadResult.getBuckets().get(0);
    List<DataSet> dataSets = bucket.getDataSets();
    for (DataSet dataSet : dataSets) {
        for (DataPoint dataPoint : dataSet.getDataPoints()) {
            Log.i("TAG","Type : " + dataPoint.getDataType().getName());
            Log.i("TAG", "StartTime : " + dateFormat.format(dataPoint.getStartTime(TimeUnit.MILLISECONDS)));
            Log.i("TAG", "EndTime : " + dateFormat.format(dataPoint.getEndTime(TimeUnit.MILLISECONDS)));
            for (Field field : dataPoint.getDataType().getFields()) {
                calories = dataPoint.getValue(field).asFloat();
                Log.i("TAG" , "Field : " + field.getName() + "Value : " + dataPoint.getValue(field));
                //mCaloriesList.add(calories, dataPoint.getStartTime(TimeUnit.MILLISECONDS));
            }
        }
    }
    return (int) calories;
}



private void setWeekTime() {
    Calendar calendar = Calendar.getInstance();
    Date now = new Date();
    calendar.setTime(now);
    endWeek = calendar.getTimeInMillis();
    calendar.add(Calendar.WEEK_OF_YEAR, -2);
    startWeek = calendar.getTimeInMillis();
}

private DataReadRequest queryFitnessDataWeekly(DataType mDataType, DataType nDataType) {
    return new DataReadRequest.Builder()
        .aggregate(mDataType, nDataType)
        .bucketByTime(1, TimeUnit.DAYS)
        .setTimeRange(startWeek, endWeek, TimeUnit.MILLISECONDS)
        .build();
}

private void caloriesCountWeekly() {
    DataReadRequest readCaloriesDataWeekly = queryFitnessDataWeekly(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED);
    Fitness.HistoryApi.readData(mClient, readCaloriesDataWeekly).setResultCallback(new ResultCallback<DataReadResult>() {
        @Override
        public void onResult(@NonNull DataReadResult dataReadResult) {
            //showCaloriesForLastWeek(dataReadResult);
            //wListener.onLastWeekCaloriesUpdated(showCalories(dataReadResult));
            showCalories(dataReadResult);
        }
    });
}

0 个答案:

没有答案