我想从中获取swimming
数据Samsung Health App
但没有得到任何适当的解决方案。下面是我的代码。
public class SwimmingReport {
private final HealthDataStore mStore;
private SwimObserver swimObserver;
private static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000L;
public SwimmingReport(HealthDataStore store) {
mStore = store;
}
public void start(SwimObserver listener, String strDate, JSONObject jsonObject) {
swimObserver = listener;
HealthDataObserver.addObserver(mStore, HealthConstants.Exercise.HEALTH_DATA_TYPE, new HealthDataObserver(null) {
@Override
public void onChange(String s) {
readTodaySwimData(strDate,jsonObject);
}
});
readTodaySwimData(strDate,jsonObject);
}
private void readTodaySwimData(String strDate,JSONObject jsonObject) {
HealthDataResolver resolver = new HealthDataResolver(mStore, null);
long startTime = GlobalMethods.getEpochTime(strDate);
long endTime = startTime + ONE_DAY_IN_MILLIS;
HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
.setDataType(HealthConstants.Exercise.HEALTH_DATA_TYPE)
.setProperties(new String[]{HealthConstants.Exercise.EXERCISE_TYPE})
.setLocalTimeRange(HealthConstants.Exercise.START_TIME, HealthConstants.Exercise.TIME_OFFSET,
startTime, endTime)
.build();
try {
resolver.read(request).setResultListener(result ->{
double distance = 0.0;
try {
for (HealthData data : result) {
distance += data.getFloat(HealthConstants.Exercise.DISTANCE);
}
} finally {
result.close();
}
if (swimObserver != null) {
swimObserver.onChanged(distance,strDate,jsonObject);
}
});
} catch (Exception e) {
Log.e("=> ", "Getting step count fails.", e);
}
}
public interface SwimObserver {
void onChanged(Double distance, String date, JSONObject jsonObject);
}
}
这是我的整个源代码