public class HealthDataResolver extends Object
Health data can be accessed with following interfaces mainly.
HealthDataResolver.InsertRequest
HealthDataResolver.ReadRequest
HealthDataResolver.UpdateRequest
HealthDataResolver.DeleteRequest
HealthDataResolver.AggregateRequest
Handling a part of data is a common use case rather than accessing whole data for the health data type. The Health data framework provides a filter to specify the precise scope for health data and is able to access required data only.
The asynchronous request is used normally for operations that take a time such as reading data with conditions.
If you need to request asynchronously, set the result listener with
HealthResultHolder.setResultListener(HealthResultHolder.ResultListener)
.
public class HealthDataResolverExample { // The state of connection HealthDataStore mStore; public static final String APP_TAG = "MyApp"; public void readGlucoseAsynchronously(long startTime, long endTime) { HealthDataResolver resolver = new HealthDataResolver(mStore, null); HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder() .setDataType(HealthConstants.BloodGlucose.HEALTH_DATA_TYPE) .setLocalTimeRange(HealthConstants.BloodGlucose.START_TIME, HealthConstants.BloodGlucose.TIME_OFFSET, startTime, endTime) .build(); try { resolver.read(request).setResultListener(mRdResult); } catch (Exception e) { Log.d(APP_TAG, "Reading health data fails."); } } private final HealthResultHolder.ResultListener<HealthDataResolver.ReadResult> mRdResult = new HealthResultHolder.ResultListener<HealthDataResolver.ReadResult>(){ @Override public void onResult(HealthDataResolver.ReadResult result) { try { Iterator<HealthData> iterator = result.iterator(); if (iterator.hasNext()) { HealthData data = iterator.next(); float glucoseValue = data.getFloat(HealthConstants.BloodGlucose.GLUCOSE); } finally { result.close(); } }; }
Though asynchronous data request is used commonly,
there is a way to get the query result synchronously with HealthResultHolder.await()
.
Note, not to make the synchronous request on the UI thread.
HealthResultHolder.await()
throws an exception if it is called on the main thread.
It can hang your application caused by taking a time to handle the synchronous query.
public class HealthDataResolverExample { // The state of connection HealthDataStore mStore; public static final String APP_TAG = "MyApp"; public void readGlucoseSynchronously(long startTime, long endTime) { HealthDataResolver resolver = new HealthDataResolver(mStore, null); HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder() .setDataType(HealthConstants.BloodGlucose.HEALTH_DATA_TYPE) .setLocalTimeRange(HealthConstants.BloodGlucose.START_TIME, HealthConstants.BloodGlucose.TIME_OFFSET, startTime, endTime) .build(); try { // Checks the result immediately HealthDataResolver.ReadResult rdResult = resolver.read(request).await(); try { Iterator<HealthData> iterator = rdResult.iterator(); if (iterator.hasNext()) { HealthData data = iterator.next(); float glucoseValue = data.getFloat(HealthConstants.BloodGlucose.GLUCOSE); } finally { rdResult.close(); } } catch (Exception e) { Log.d(APP_TAG, "Reading health data fails."); } } }
Getting data permission is required after connection to the health data store to synchronize health data with Samsung Health.
Samsung Health SDK for Android provides the data permission UI to get the user's consent.
The user can agree to share health data with the application through the permission UI and the application can get required data permission
Check HealthPermissionManager
to use the permission UI.
If you try to read or write health data without permission, the SDK gives SecurityException
.
Gaining the HealthConstants.HealthDocument
type's permission is different with other data types.
It is available by acquiring the instant permission.
Instant permission is created for one-time data access. It is proper to handle very sensitive health data.
The data type needs the instant permission is HealthConstants.HealthDocument
.
An app needs the instant permission whenever it accesses health document data.
HealthDataResolver.insertWithPermission()
HealthDataResolver.readWithPermission()
HealthDataResolver.deleteWithPermission()
HealthDataResolver.readWithPermission()
, its permission pop-up is shown.HealthDataResolver
with other classes.
Modifier and Type | Class and Description |
---|---|
static interface |
HealthDataResolver.AggregateRequest
This interface represents an aggregate request with aggregate functions and the specified time unit to group result values.
|
static class |
HealthDataResolver.AggregateResult
This class represents the result of
HealthDataResolver.AggregateRequest . |
static interface |
HealthDataResolver.DeleteRequest
This interface is able to make a request to delete health data for the specific health data type.
|
static class |
HealthDataResolver.Filter
This class creates a filter to make the request range clear.
|
static interface |
HealthDataResolver.InsertRequest
This interface is able to make a request to insert health data for the specific health data type.
|
static interface |
HealthDataResolver.ReadRequest
This interface is able to make a request to read health data for the specific health data type.
|
static class |
HealthDataResolver.ReadResult
This class represents the result for
HealthDataResolver.ReadRequest . |
static class |
HealthDataResolver.SortOrder
This enum defines sort orders.
|
static interface |
HealthDataResolver.UpdateRequest
This interface is able to make a request to update health data for the specific health data type.
|
Constructor and Description |
---|
HealthDataResolver(HealthDataStore store,
Handler handler)
Creates a HealthDataResolver instance.
|
public HealthDataResolver(HealthDataStore store, Handler handler)
store
- The health data store for connectionhandler
- The handler for the proceeding thread.
If it's null
, the looper of the current thread will be used.public HealthResultHolder<HealthResultHolder.BaseResult> insert(HealthDataResolver.InsertRequest request)
Check this request's result in HealthResultHolder.BaseResult.getStatus()
.
request
- The request to insert new health dataHealthResultHolder
instance which resolves to the HealthResultHolder.BaseResult
for inserted health dataIllegalArgumentException
- If the request
contains invalid instance type or null
.SecurityException
- If there is no permission to write for given health dataIllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionpublic HealthResultHolder<HealthResultHolder.BaseResult> insertWithPermission(HealthDataResolver.InsertRequest request, Activity activity)
Check this request's result in HealthResultHolder.BaseResult.getStatus()
.
request
- The request to insert new health dataactivity
- The activity that pop up the permission UIHealthResultHolder
instance which resolves to the HealthResultHolder.BaseResult
for inserted health dataIllegalArgumentException
- If the request
contains invalid instance typeor null
.IllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionpublic HealthResultHolder<HealthResultHolder.BaseResult> update(HealthDataResolver.UpdateRequest request)
Check this request's result through HealthResultHolder.BaseResult.getStatus()
.
request
- The request to update health dataHealthResultHolder
instance which resolves to the HealthResultHolder.BaseResult
for updated health dataIllegalArgumentException
- If the request
contains invalid instance typeor null
.SecurityException
- If there is no permission to write for given health dataIllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionpublic HealthResultHolder<HealthResultHolder.BaseResult> delete(HealthDataResolver.DeleteRequest request)
request
- The request to delete health dataHealthResultHolder
instance which resolves to the HealthResultHolder.BaseResult
for deleted health dataIllegalArgumentException
- If the request
contains invalid instance type or null
SecurityException
- If there is no permission to write for given health dataIllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionpublic HealthResultHolder<HealthResultHolder.BaseResult> deleteWithPermission(HealthDataResolver.DeleteRequest request, Activity activity)
request
- The request to delete health dataactivity
- The activity that pop up the permission UIHealthResultHolder
instance which resolves to the HealthResultHolder.BaseResult
for deleted health dataIllegalArgumentException
- If the request
contains invalid instance type or null
IllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionpublic HealthResultHolder<HealthDataResolver.ReadResult> read(HealthDataResolver.ReadRequest request)
request
- The request to read health dataHealthResultHolder
instance which resolves to the HealthDataResolver.ReadResult
for health data to readIllegalArgumentException
- If the request
contains invalid instance type or null
SecurityException
- If there is no permission to read for given health dataIllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionpublic HealthResultHolder<HealthDataResolver.ReadResult> readWithPermission(HealthDataResolver.ReadRequest request, Activity activity)
request
- The request to read health dataactivity
- The activity that pop up the permission UIHealthResultHolder
instance which resolves to the HealthDataResolver.ReadResult
for health data to readIllegalArgumentException
- If the request
contains invalid instance type or null
IllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionpublic HealthResultHolder<HealthDataResolver.AggregateResult> aggregate(HealthDataResolver.AggregateRequest request)
request
- The aggregate request for health dataHealthDataResolver.AggregateResult
instance which resolves to the HealthDataResolver.ReadResult
for the requested aggregateIllegalArgumentException
- If the request
contains invalid instance type or null
SecurityException
- If there is no permission to read for given health dataIllegalStateException
- If the instance of health data store is invalid or a remote-invocation error occurs on the connectionCopyright © Samsung Electronics, Co., Ltd. All rights reserved.