我有一个从三星健康读取健康数据并浏览它的安卓应用程序。我添加了 Samsung Health sdk 版本 1.4.0 并开始使用 HealthDataResolver.ReadRequest 获取数据,以读取不同的数据值,例如总步数、距离和活动时间。我可以从 HealthConstants.StepCount 读取步数和距离。我阅读了所有的步数记录并计算了所有这些记录值的总和,但结果等于选择手机+所有步数时的步数。我的问题是如何选择我想要获取的数据(手机或所有步骤)。
private void readSteps(){
HealthDataResolver resolver = new HealthDataResolver(mStore, null);
HealthDataResolver.Filter filter = null;
if(mReadDataRequest.getStartTime() != 0.0)
filter = HealthDataResolver.Filter.greaterThanEquals(HealthConstants.StepCount.START_TIME, mReadDataRequest.getStartTime());
HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
.setDataType(HealthConstants.StepCount.HEALTH_DATA_TYPE)
.setTimeBefore(Calendar.getInstance().getTimeInMillis())
.setFilter(filter)
.build();
try {
resolver.read(request).setResultListener(result -> {
ArrayList<BaseHealthKit> stepCounts = null;
try {
for (HealthData data : result) {
int count = data.getInt(HealthConstants.StepCount.COUNT);
double distance = data.getDouble(HealthConstants.StepCount.DISTANCE);
String startTime = data.getString(HealthConstants.StepCount.START_TIME);
String endTime = data.getString(HealthConstants.StepCount.END_TIME);
float burnedCalorie = data.getFloat(HealthConstants.StepCount.CALORIE);
if (stepCounts == null)
stepCounts = new ArrayList<>();
StepCount stepCount = new StepCount();
if (startTime != null)
stepCount.setStartTime(Long.parseLong(startTime));
if (endTime != null)
stepCount.setEndTime(Long.parseLong(endTime));
stepCount.setCount(count);
stepCount.setDistance(distance);
stepCount.setmBurnedCalories(burnedCalorie);
stepCounts.add(stepCount);
}
} finally {
result.close();
}
if (mReadDataListener != null)
mReadDataListener.onSucceedReading(stepCounts);
});
} catch (Exception e) {
Log.e(TAG, "Getting sleep data fails.", e);
} finally {
disconnect();
}
}