如何从Google Fit中读取消耗的卡路里和剩余卡路里。到现在为止,我仅能获得步数,而且我还需要卡路里数据。我还将体重和身高插入Hitory api,如下所示。
public void saveUserHeight(int heightCentimiters) {
// to post data
float height = ((float) heightCentimiters) / 100.0f;
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
DataSet heightDataSet = createDataForRequest(
DataType.TYPE_HEIGHT, // for height, it would be DataType.TYPE_HEIGHT
DataSource.TYPE_RAW,
height, // weight in kgs
startTime, // start time
endTime, // end time
TimeUnit.MILLISECONDS // Time Unit, for example, TimeUnit.MILLISECONDS
);
com.google.android.gms.common.api.Status heightInsertStatus =
Fitness.HistoryApi.insertData(mGoogleApiFitnessClient, heightDataSet)
.await(1, TimeUnit.MINUTES);
if(heightInsertStatus.isSuccess()){
Log.e("Height","Inserted");
}
else{
Log.e("Height","inserted failed");
}
}
public void saveUserWeight(float weight) {
// to post data
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
DataSet weightDataSet = createDataForRequest(
DataType.TYPE_WEIGHT, // for height, it would be DataType.TYPE_HEIGHT
DataSource.TYPE_RAW,
weight, // weight in kgs
startTime, // start time
endTime, // end time
TimeUnit.MILLISECONDS // Time Unit, for example, TimeUnit.MILLISECONDS
);
com.google.android.gms.common.api.Status weightInsertStatus =
Fitness.HistoryApi.insertData(mGoogleApiFitnessClient, weightDataSet)
.await(1, TimeUnit.MINUTES);
if(weightInsertStatus.isSuccess()){
Log.e("Weight","Inserted");
}
else{
Log.e("Weight","inserted failed");
}
}
为了得到今天的步数,我使用了这种方法。但是对于卡路里信息,我没有找到方法..因此,如果有人知道请在这里分享它..
private void getStepsToday() {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
final DataReadRequest readRequest = new DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
DataReadResult dataReadResult =
Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);
DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
int totalSteps = 0;
for (DataPoint dp : stepData.getDataPoints()) {
for(Field field : dp.getDataType().getFields()) {
int steps = dp.getValue(field).asInt();
totalSteps += steps;
}
}
publishTodaysStepData(totalSteps);
}
最佳答案
对于卡路里,您需要更改数据类型
即:
DataType.TYPE_CALORIES_EXPENDED
要么:
DailyTotalResult result = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient,DataType.TYPE_CALORIES_EXPENDED).await(1,TimeUnit.MINUTES);