com.samsung.android.sdk.healthdata
Interface HealthDataResolver.AggregateRequest
-
- Enclosing class:
- HealthDataResolver
public static interface HealthDataResolver.AggregateRequest
This interface represents an aggregate request with aggregate functions and the specified time unit to group result values.Request for Aggregating Data
HealthDataResolver.aggregate() helps that your application aggregates Samsung Health's data.
The following information is mandatory to build anAggregateRequest
instance.Mandatory information Description Data type Set the data type with:
Aggregate function Add an aggregate function with:
You can make a request instance with
HealthDataResolver.AggregateRequest.Builder.build()
.Example: Aggregate Function
To know the average body temperature by tags, you can set an
AggregateRequest
object with:setDataType()
addFunction()
addGroup()
The aggregate request can be sent with a listener asynchronously.public class HealthDataResolverExample { // The state of connection private HealthDataStore mStore; public static final String APP_TAG = "MyApp"; private void getAverageTemperatureByTag(long startTime, long endTime) { HealthDataResolver.AggregateRequest request = new HealthDataResolver.AggregateRequest.Builder() .setDataType(HealthConstants.BodyTemperature.HEALTH_DATA_TYPE) .addFunction(HealthDataResolver.AggregateRequest.AggregateFunction.AVG, HealthConstants.BodyTemperature.TEMPERATURE, "avr_temperature") .addGroup(HealthConstants.BodyTemperature.COMMENT, "tag") .setLocalTimeRange(HealthConstants.BodyTemperature.START_TIME, HealthConstants.BodyTemperature.TIME_OFFSET, startTime, endTime) .build();
The result includes following values:HealthDataResolver resolver = new HealthDataResolver(mStore, null); try { resolver.aggregate(request).setResultListener(mTempAggrResult); } catch (Exception e) { Log.d(APP_TAG, "Aggregating health data fails."); } }
- grouped with
COMMENT
astag
alias - average temperature value for each
tag
asavr_temperature
alias
private final HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult> mTempAggrResult= new HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult>() { @Override public void onResult(HealthDataResolver.AggregateResult result) { try { Iterator<HealthData> iterator = result.iterator(); if (iterator.hasNext()) { HealthData data = iterator.next(); // Gets the tag name String tag = data.getString("tag"); // Gets the average temperature float temperature = data.getFloat("avr_temperature"); // ... } } finally { result.close(); } } }; }
Example: Aggregate Function and Time Group
You can set an
AggregateRequest
object with:setDataType()
addFunction()
setTimeGroup()
setSort()
AggregateRequest
object as the following example.
The aggregate request can be sent with a listener asynchronously.public class HealthDataResolverExample { // The state of connection private HealthDataStore mStore; public static final String APP_TAG = "MyApp"; private void getDailyGlucose(long startTime, long endTime) { HealthDataResolver.AggregateRequest request = new HealthDataResolver.AggregateRequest.Builder() .setDataType(HealthConstants.BloodGlucose.HEALTH_DATA_TYPE) .addFunction(HealthDataResolver.AggregateRequest.AggregateFunction.AVG, HealthConstants.BloodGlucose.GLUCOSE, "average") .setTimeGroup(HealthDataResolver.AggregateRequest.TimeGroupUnit.DAILY, 1, HealthConstants.BloodGlucose.START_TIME, HealthConstants.BloodGlucose.TIME_OFFSET, "day") .setLocalTimeRange(HealthConstants.BloodGlucose.START_TIME, HealthConstants.BloodGlucose.TIME_OFFSET, startTime, endTime) .setSort("days", HealthDataResolver.SortOrder.DESC) .build();
The result includes following values:HealthDataResolver resolver = new HealthDataResolver(mStore, null); try { resolver.aggregate(request).setResultListener(mGlucoseAggrResult); } catch (Exception e) { Log.d(APP_TAG, "Aggregating health data fails."); } }
- grouped with
DAILY
time unit asday
alias - average blood glucose value for each
day
asaverage
alias - sorted with
DESC
private final HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult> mGlucoseAggrResult = new HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult>() { @Override public void onResult(HealthDataResolver.AggregateResult result) { try { Iterator<HealthData> iterator = result.iterator(); if (iterator.hasNext()) { HealthData data = iterator.next(); // Gets day information String tag = data.getString("day"); // Gets the average glucose float avg = data.getFloat("average"); // ... } } finally { result.close(); } }; }
- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface and Description static class
HealthDataResolver.AggregateRequest.AggregateFunction
This enumeration defines useful aggregate functions.static class
HealthDataResolver.AggregateRequest.Builder
This class creates anHealthDataResolver.AggregateRequest
's instance for the specified data type.static class
HealthDataResolver.AggregateRequest.TimeGroupUnit
This enumeration defines time units to group values of the aggregate result.
-