营养数据 google 适合

nutrition data google fit

读取营养数据有延迟吗?我插入了营养数据并试图读取它们,但数据点的大小仍然等于 0...

我插入的数据是这样的:

long now = System.currentTimeMillis();

DataSource nutritionSource = new DataSource.Builder()
            .setAppPackageName(getApplicationContext().getPackageName())
            .setType(DataSource.TYPE_RAW)
            .setDataType(DataType.TYPE_NUTRITION)
            .build();

DataSet dataSet = DataSet.create(nutritionSource);

DataPoint dataPoint = DataPoint.create(nutritionSource);
dataPoint.setTimestamp(now, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_FOOD_ITEM).setString(name_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_CALORIES, calorie_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_SUGAR,sugar_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_TOTAL_FAT,fat_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_PROTEIN,protein_food);
dataPoint.getValue(Field.FIELD_MEAL_TYPE).setInt(Field.MEAL_TYPE_UNKNOWN);


dataSet.add(dataPoint);

然后,我试着像这样阅读它们:

DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_NUTRITION, DataType.AGGREGATE_NUTRITION_SUMMARY)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    // Invoke the History API to fetch the data with the query and await the result of
    // the read request.
    DataReadResult dataReadResult =
            Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);

    if(dataReadResult.getStatus().isSuccess()){
        Log.i("TAG","isSuccess to read nutrition data");
        if(dataReadResult.getDataSet(DataType.TYPE_NUTRITION).getDataPoints().size() > 0){
            Log.i("TAG","calorie : "+dataReadResult.getDataSet(DataType.TYPE_NUTRITION).getDataPoints().get(0).getValue(Field.FIELD_CALORIES));
        }
    }

我想获取当天的所有数据点并使用它们,但我不知道怎么做??

谢谢,

卡梅尔

不确定这对您来说是否仍然是个问题,但尝试使用它来获取每个 "bucket" 的聚合数据。

        List<Bucket> buckets = dataReadResult.getBuckets();
        for (int a = buckets.size() - 1; a > -1; a--) {
            Bucket bucket = buckets.get(a);
            for (DataSet dataSet : bucket.getDataSets()) {
                if(dataSet.getDataType().getName().equals(DataType.TYPE_NUTRITION.getName())){
                    for (DataPoint dp : dataSet.getDataPoints()) {
                        Log.i(TAG, "Data point:");
                        Log.i(TAG, "\tType: " + dp.getDataType().getName());
                        for(Field field : dp.getDataType().getFields()) {
                            Log.i(TAG, "\tField: " + field.getName() +
                                    " Value: " + dp.getValue(field));
                        }
                    }
                }
            }
        }