This interface is able to make a request to read health data for the specific health data type.
Request for Reading Data
HealthDataResolver.read() helps that your application reads
Samsung Health's data.
The following information is mandatory to build a
ReadRequest instance.
| Mandatory information |
Description |
| Data type |
Set the data type with:
|
You can make a request instance with
ReadRequest.Builder.build().
Reading Data Example
You can read Samsung Health's health data with
HealthDataResolver.read().
The filter below is created to read today's step count.
The time range is set from the
startTime to
endTime.
// The state of connection
private final HealthDataStore mStore;
public static final String APP_TAG = "MyApp";
private static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000L;
private long getStartTimeOfToday() {
Calendar today = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
return today.getTimeInMillis();
}
An
ReadRequest instance is created by
ReadRequest.Builder.build() with setting
the data type and the range for reading data.
private void readTodayStepCount() {
// Set time range from start time of today to the current time
long startTime = getStartTimeOfToday();
long endTime = startTime + ONE_DAY_IN_MILLIS;
HealthDataResolver.ReadRequest request = new ReadRequest.Builder()
.setDataType(HealthConstants.StepCount.HEALTH_DATA_TYPE)
.setProperties(new String[] {HealthConstants.StepCount.COUNT})
.setLocalTimeRange(HealthConstants.StepCount.START_TIME, HealthConstants.StepCount.TIME_OFFSET,
startTime, endTime)
.build();
ReadRequest.read() sends a reading request to
Samsung Health.
The asynchronous request is used generally by setting a listener.
HealthDataResolver resolver = new HealthDataResolver(mStore, null);
try {
resolver.read(request).setResultListener(mListener);
} catch (Exception e) {
Log.d(MyApp, "Getting step count fails.");
}
}
The result is sent to the implemented listener.
The today's total count is returned by adding all
COUNT values of the result.
private final HealthResultHolder.ResultListener<ReadResult> mListener =
new HealthResultHolder.ResultListener<ReadResult>() {
@Override
public void onResult(ReadResult result) {
int count = 0;
try {
Iterator<HealthData> iterator = result.iterator();
if (iterator.hasNext()) {
HealthData data = iterator.next();
count += data.getInt(HealthConstants.StepCount.COUNT);
}
} finally {
result.close();
}
}
};