首页 > 解决方案 > google fit 中的卡路里值错误

问题描述

我正在尝试获取一天中燃烧的卡路里数量,这是在日历中设置的日期。问题是,即使我从未来选择一天,每天的卡路里数值也在 1400 到 1500 之间。

private void updateInfo() {

final GoogleApiClient client = new GoogleApiClient.Builder(getContext())
        .addApi(Fitness.HISTORY_API)
        .build();

client.connect();


final PendingResult<DataReadResult> dataReadResultPendingResult = Fitness.HistoryApi.readData(client, new DataReadRequest.Builder()
        .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
        .bucketByTime(1, TimeUnit.DAYS)
        .aggregate(TYPE_CALORIES_EXPENDED, AGGREGATE_CALORIES_EXPENDED)
        .build());


final Handler handler = new Handler();
new Thread() {
    @Override
    public void run() {
        final DataReadResult totalResult = dataReadResultPendingResult.await(30, SECONDS);
        if (totalResult.getStatus().isSuccess()) {

            handler.post(new Thread() {
                @Override
                public void run() {
                    quantityCaloriesSpent.setText(totalResult.getBuckets().get(0).getDataSets().get(0).getDataPoints().get(0).getValue(FIELD_CALORIES).asFloat()+ "");
                }
            });

        } else {
            // handle failure
        }
    }
}.start();

}

标签: androidgoogle-api-clientgoogle-fitandroid-googleapiclient

解决方案


推荐阅读