Google Fit API中有各种各样的健身活动,如健美操,羽毛球,拳击,举重等。如何使用Google Fit API获取所计算步数和每项活动所消耗的卡路里?任何形式的建议都是最受欢迎的。提前谢谢。
答案 0 :(得分:0)
我不确定这个结果是否符合您的要求。
您可以使用bucketByActivitySegement
下的DataReadRequest.Builder
分割每个活动广告。
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
.bucketByActivitySegment( 1, TimeUnit.MINUTES) // just segement time over 1 minute will be list
.setTimeRange(startTimeLong, endTimeLong, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build();
Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context))
.readData(readRequest)
.addOnSuccessListener()
.addOnFailureListener();
结果你将得到几个桶,每个桶是一个活动段。存储桶包含两个DataSet。一个是步骤值,另一个是卡路里值。如下所示:
Bucket 0: activity: still, 05/09 00:00~08:03
-DataSet 0:
--DataPoint 0: 05/09 08:02~09:03
---Field name: steps, value: 55
-DataSet 1:
--DataPoint 0: 05/09 00:00~08:03
---Field name: calories, value: 459.41046
Bucket 1: acitvity: walking, 05/09 08:03~08:09
-DataSet 0:
--DataPoint 0: 05/09 08:03~08:04
---Field name: steps, value: 181
-DataSet: 1
--DataPoint 0: 05/09 08:03~08:09
---Field name: calories, value: 20.808548
Bucket 2: activity: in_vehicle, 05/09 08:09~08:48
-DataSet 0:
--DataPoint 0: 05/09 08:33~08:38
---Field name: steps, value: 156
-DataSet 1:
--DataPoint 0: 05/09 08:09~08:48
---Field name: calories, value: 36.633526
Bucket 3: activity: walking, 05/09 08:48~09:12
...
...
...
如果在一段时间内每种类型的汇总值需要的结果,您可以使用bucketByActivityType
。
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
.bucketByActivityType( 1, TimeUnit.MINUTES)
.setTimeRange(startTimeLong, endTimeLong, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build();
它将在TimeRange期间返回每种类型的总值。
Bucket 0: activity: still, 05/09 00:00 ~ 05/16 18:42
-DataSet 0:
--DataPoint 0: 05/09 00:00 ~ 05/16 18:42
---Field name: calories, value: 9984.753
-DataSet 1:
--DataPoint 0: 05/09 08:02 ~ 05/16 17:24
---Field name: steps, value: 6499
Bucket 1: acitvity: walking, 05/09 08:03 ~ 05/16 13:28
-DataSet 0:
--DataPoint 0: 05/09 08:03 ~ 05/16 13:28
---Field name: calories, value: 1214.7212
-DataSet: 1
--DataPoint 0: 05/09 08:03 ~ 05/16 13:20
---Field name: steps, value: 12750
Bucket 2: activity: in_vehicle, 05/09 08:09 ~ 05/09 08:48
-DataSet 0:
--DataPoint 0: 05/09 08:09 ~ 05/09 08:48
---Field name: calories, value: 36.633526
-DataSet 1:
--DataPoint 0: 05/09 08:33 ~ 05/09 08:38
---Field name: steps, value: 156
Bucket 3: activity: aerobics, 05/11 18:00 ~ 05/11 18:59
...
...
...
希望这会对你有所帮助。
--- 2018/05/30更新---
在addOnSuccessListener()
中,您可以添加如下监听器:
new OnSuccessListener<DataReadResponse>() {
@Override
public void onSuccess(DataReadResponse dataReadResult) {
String returnValue = "";
if (dataReadResult.getBuckets().size() > 0) {
for (int i = 0; i < dataReadResult.getBuckets().size(); i++) {
returnValue += "\n\n" + i + " ---new bucket-- activity: " + dataReadResult.getBuckets().get(i).getActivity() + "\n"
+ getNoYearDateWithTimeFormat().format(dataReadResult.getBuckets().get(i).getStartTime(TimeUnit.MILLISECONDS)) + "~"
+ getNoYearDateWithTimeFormat().format(dataReadResult.getBuckets().get(i).getEndTime(TimeUnit.MILLISECONDS));
for (int j = 0; j < dataReadResult.getBuckets().get(i).getDataSets().size(); j++) {
returnValue += "\n-data set " + j + "-package: " + dataReadResult.getBuckets().get(i).getDataSets().get(j).getDataSource().getAppPackageName() + ", stream: " + dataReadResult.getBuckets().get(i).getDataSets().get(j).getDataSource().getStreamIdentifier();
returnValue += handleDailyRecordInDataSet(dataReadResult.getBuckets().get(i).getDataSets().get(j));
}
}
}
if (fitnessListener != null) {
fitnessListener.readActionDone(null, returnValue);
}
}
}
在每个存储桶中,您可以为每种数据类型获取多个DataSet。
private String handleDailyRecordInDataSet(DataSet dataSet) {
String returnValue = "";
for (DataPoint dataPoint : dataSet.getDataPoints()) {
long startTime = dataPoint.getStartTime(TimeUnit.MILLISECONDS);
long endTime = dataPoint.getEndTime(TimeUnit.MILLISECONDS);
String tempValue = "DataPoint start: " + getDateWithTimeFormat().format(new Date(startTime))
+ ", end=" + getDateWithTimeFormat().format(new Date(endTime))
+ ", type=" + dataPoint.getDataType().getName() + ", package=" + dataPoint.getDataSource().getAppPackageName() + ", stream=" + dataPoint.getDataSource().getStreamIdentifier();
if (dataPoint.getDataSource().getDevice() != null) {
tempValue += "\nManufacturer=" + dataPoint.getDataSource().getDevice().getManufacturer() + ", model=" + dataPoint.getDataSource().getDevice().getModel()
+ ", uid: " + dataPoint.getDataSource().getDevice().getUid() + ", type=" + dataPoint.getDataSource().getDevice().getType();
}
tempValue += "\norigin source: package=" + dataPoint.getOriginalDataSource().getAppPackageName() + ", stream=" + dataPoint.getOriginalDataSource().getStreamIdentifier();
if (dataPoint.getOriginalDataSource().getDevice() != null) {
tempValue += "\nManufacturer=" + dataPoint.getOriginalDataSource().getDevice().getManufacturer() + ", model=" + dataPoint.getOriginalDataSource().getDevice().getModel()
+ ", uid: " + dataPoint.getOriginalDataSource().getDevice().getUid() + ", type=" + dataPoint.getOriginalDataSource().getDevice().getType();
}
returnValue += ("\n\n" + tempValue);
for (Field field : dataPoint.getDataType().getFields()) {
String fieldValue = "Field name: " + field.getName() + ", value: " + dataPoint.getValue(field);
returnValue += ("\n" + fieldValue);
}
}
return returnValue;
}