如何将有关食用食物的营养信息添加到 google 适合 android 的历史记录中?
How to add nutrion information about consumed food to google fit history on android?
我想让我的应用程序的用户添加有关他们所吃食物的信息,并且我需要将营养信息保存到 google 健身历史记录中。我已连接到 google 播放服务,我希望我请求正确的 API 和范围。
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE))
...
但是找不到任何好的示例如何将包含所有营养信息的数据添加到用户 google 适合历史记录。
我想我需要使用 com.google.nutrition DATA_TYPE 并以某种方式填写所有字段,但是没有代码片段可以准确地告诉我如何做到这一点。
感谢您的帮助
原来google fit doc里还是有东西的。
创建你的香蕉:
DataSource nutritionSource = new DataSource.Builder()
.setDataType(TYPE_NUTRITION)
...
.build();
DataPoint banana = DataPoint.create(nutritionSource);
banana.setTimestamp(now.getMillis(), MILLISECONDS);
banana.getValue(FIELD_FOOD_ITEM).setString("banana");
banana.getValue(FIELD_MEAL_TYPE).setInt(MEAL_TYPE_SNACK);
banana.getValue(FIELD_NUTRIENTS).setKeyValue(NUTRIENT_TOTAL_FAT, 0.4f);
banana.getValue(FIELD_NUTRIENTS).setKeyValue(NUTRIENT_SODIUM, 1f);
banana.getValue(FIELD_NUTRIENTS).setKeyValue(NUTRIENT_POTASSIUM, 422f);
那么你可能想要拯救你的香蕉:
DataSet dataSet = DataSet.create(nutritionSource);
dataSet.add(banana);
PendingResult<Status> result = Fitness.HistoryApi.insertData(mClient, dataSet);
读一些香蕉:
// Somewhere in your class definitions
private static final String TAG = "NutritionHistory";
private static final String[] NUTRIENTS_ARRAY = new String[] {
Field.NUTRIENT_CALORIES,
Field.NUTRIENT_TOTAL_FAT,
Field.NUTRIENT_SATURATED_FAT,
Field.NUTRIENT_UNSATURATED_FAT,
Field.NUTRIENT_POLYUNSATURATED_FAT,
Field.NUTRIENT_MONOUNSATURATED_FAT,
Field.NUTRIENT_TRANS_FAT,
Field.NUTRIENT_CHOLESTEROL,
Field.NUTRIENT_SODIUM,
Field.NUTRIENT_POTASSIUM,
Field.NUTRIENT_TOTAL_CARBS,
Field.NUTRIENT_DIETARY_FIBER,
Field.NUTRIENT_SUGAR,
Field.NUTRIENT_PROTEIN,
Field.NUTRIENT_VITAMIN_A,
Field.NUTRIENT_VITAMIN_C,
Field.NUTRIENT_CALCIUM,
Field.NUTRIENT_IRON
};
// Then for reading data
public someMethodForReading(long startTime, long endTime) {
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_NUTRITION, DataType.AGGREGATE_NUTRITION_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();
DataReadResult dataReadResult = Fitness.HistoryApi.readData(googleFitManager.getGoogleApiClient(), readRequest)
.await(1, TimeUnit.MINUTES);
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
// Getting individual datapoints (one for each date)
for (DataPoint dp : dataSet.getDataPoints()) {
Date date = new Date(dp.getStartTime(TimeUnit.MILLISECONDS));
String foodItem = dp.getValue(FIELD_FOOD_ITEM).asString();
int mealType = dp.getValue(FIELD_MEAL_TYPE).asInt();
Value nutrients = dp.getValue((Field.FIELD_NUTRIENTS));
HashMap<String, Float> nutrients = getNutrientsAsMap(nutrients);
// Do something with your data
// ...
}
}
}
}
// The method where the 'magic' happens
private HashMap<String, Float> getNutrientsAsMap(Value nutrients) {
HashMap<String, Float> nutrientsMap = new HashMap<>();
for (String nutrientKey : NUTRIENTS_SET) {
try {
Float nutrientVal = nutrients.getKeyValue(nutrientKey);
nutrientsMap.put(nutrientKey, nutrientVal);
} catch (Exception e) {
}
}
return nutrientsMap;
}
我想让我的应用程序的用户添加有关他们所吃食物的信息,并且我需要将营养信息保存到 google 健身历史记录中。我已连接到 google 播放服务,我希望我请求正确的 API 和范围。
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE))
...
但是找不到任何好的示例如何将包含所有营养信息的数据添加到用户 google 适合历史记录。 我想我需要使用 com.google.nutrition DATA_TYPE 并以某种方式填写所有字段,但是没有代码片段可以准确地告诉我如何做到这一点。
感谢您的帮助
原来google fit doc里还是有东西的。 创建你的香蕉:
DataSource nutritionSource = new DataSource.Builder()
.setDataType(TYPE_NUTRITION)
...
.build();
DataPoint banana = DataPoint.create(nutritionSource);
banana.setTimestamp(now.getMillis(), MILLISECONDS);
banana.getValue(FIELD_FOOD_ITEM).setString("banana");
banana.getValue(FIELD_MEAL_TYPE).setInt(MEAL_TYPE_SNACK);
banana.getValue(FIELD_NUTRIENTS).setKeyValue(NUTRIENT_TOTAL_FAT, 0.4f);
banana.getValue(FIELD_NUTRIENTS).setKeyValue(NUTRIENT_SODIUM, 1f);
banana.getValue(FIELD_NUTRIENTS).setKeyValue(NUTRIENT_POTASSIUM, 422f);
那么你可能想要拯救你的香蕉:
DataSet dataSet = DataSet.create(nutritionSource);
dataSet.add(banana);
PendingResult<Status> result = Fitness.HistoryApi.insertData(mClient, dataSet);
读一些香蕉:
// Somewhere in your class definitions
private static final String TAG = "NutritionHistory";
private static final String[] NUTRIENTS_ARRAY = new String[] {
Field.NUTRIENT_CALORIES,
Field.NUTRIENT_TOTAL_FAT,
Field.NUTRIENT_SATURATED_FAT,
Field.NUTRIENT_UNSATURATED_FAT,
Field.NUTRIENT_POLYUNSATURATED_FAT,
Field.NUTRIENT_MONOUNSATURATED_FAT,
Field.NUTRIENT_TRANS_FAT,
Field.NUTRIENT_CHOLESTEROL,
Field.NUTRIENT_SODIUM,
Field.NUTRIENT_POTASSIUM,
Field.NUTRIENT_TOTAL_CARBS,
Field.NUTRIENT_DIETARY_FIBER,
Field.NUTRIENT_SUGAR,
Field.NUTRIENT_PROTEIN,
Field.NUTRIENT_VITAMIN_A,
Field.NUTRIENT_VITAMIN_C,
Field.NUTRIENT_CALCIUM,
Field.NUTRIENT_IRON
};
// Then for reading data
public someMethodForReading(long startTime, long endTime) {
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_NUTRITION, DataType.AGGREGATE_NUTRITION_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();
DataReadResult dataReadResult = Fitness.HistoryApi.readData(googleFitManager.getGoogleApiClient(), readRequest)
.await(1, TimeUnit.MINUTES);
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
// Getting individual datapoints (one for each date)
for (DataPoint dp : dataSet.getDataPoints()) {
Date date = new Date(dp.getStartTime(TimeUnit.MILLISECONDS));
String foodItem = dp.getValue(FIELD_FOOD_ITEM).asString();
int mealType = dp.getValue(FIELD_MEAL_TYPE).asInt();
Value nutrients = dp.getValue((Field.FIELD_NUTRIENTS));
HashMap<String, Float> nutrients = getNutrientsAsMap(nutrients);
// Do something with your data
// ...
}
}
}
}
// The method where the 'magic' happens
private HashMap<String, Float> getNutrientsAsMap(Value nutrients) {
HashMap<String, Float> nutrientsMap = new HashMap<>();
for (String nutrientKey : NUTRIENTS_SET) {
try {
Float nutrientVal = nutrients.getKeyValue(nutrientKey);
nutrientsMap.put(nutrientKey, nutrientVal);
} catch (Exception e) {
}
}
return nutrientsMap;
}