[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

HealthConnectManager  |  Android Developers

The Wayback Machine - https://web.archive.org/web/20230208230451/https://developer.android.com/reference/android/healthconnect/HealthConnectManager

Stay organized with collections Save and categorize content based on your preferences.

public class HealthConnectManager
extends Object



This class provides APIs to interact with the centralized HealthConnect storage maintained by the system.

HealthConnect is an offline, on-device storage that unifies data from multiple devices and apps into an ecosystem featuring.

The basic unit of data in HealthConnect is represented as a Record object, which is the base class for all the other data types such as StepsRecord.

Summary

Constants

String ACTION_MANAGE_HEALTH_PERMISSIONS

Activity action: Launch UI to manage (e.g. grant/revoke) health permissions.

String CATEGORY_HEALTH_PERMISSIONS

Used in conjunction with Intent.ACTION_VIEW_PERMISSION_USAGE to launch UI to show an app’s health permission rationale/data policy.

Public methods

<T> void aggregate(AggregateRecordsRequest<T> request, Executor executor, OutcomeReceiver<AggregateRecordsResponse<T>, HealthConnectException> callback)

Get aggregations corresponding to request.

<T> void aggregateGroupByDuration(AggregateRecordsRequest<T> request, Duration duration, Executor executor, OutcomeReceiver<List<AggregateRecordsGroupedByDurationResponse<T>>, HealthConnectException> callback)

Get aggregations corresponding to request.

<T> void aggregateGroupByPeriod(AggregateRecordsRequest<T> request, Period period, Executor executor, OutcomeReceiver<List<AggregateRecordsGroupedByPeriodResponse<T>>, HealthConnectException> callback)

Get aggregations corresponding to request.

void deleteRecords(Class<? extends Record> recordType, TimeRangeFilter timeRangeFilter, Executor executor, OutcomeReceiver<VoidHealthConnectException> callback)

Deletes records based on the TimeRangeFilter.

void deleteRecords(List<RecordIdFilter> recordIds, Executor executor, OutcomeReceiver<VoidHealthConnectException> callback)

Deletes records based on RecordIdFilter.

void getChangeLogToken(ChangeLogTokenRequest request, Executor executor, OutcomeReceiver<StringHealthConnectException> callback)

Get token for {HealthConnectManager#getChangeLogs}.

void getChangeLogs(ChangeLogsRequest changeLogsRequest, Executor executor, OutcomeReceiver<ChangeLogsResponseHealthConnectException> callback)

Get change logs post the time when token was generated.

void insertRecords(List<Record> records, Executor executor, OutcomeReceiver<InsertRecordsResponseHealthConnectException> callback)

Inserts records into the HealthConnect database.

<T extends Record> void readRecords(ReadRecordsRequestUsingIds<T> request, Executor executor, OutcomeReceiver<ReadRecordsResponse<T>, HealthConnectException> callback)

API to read records based on RecordIdFilter.

<T extends Record> void readRecords(ReadRecordsRequestUsingFilters<T> request, Executor executor, OutcomeReceiver<ReadRecordsResponse<T>, HealthConnectException> callback)

API to read records based on ReadRecordsRequestUsingFilters.

void updateRecords(List<Record> records, Executor executor, OutcomeReceiver<VoidHealthConnectException> callback)

Updates records into the HealthConnect database.

Inherited methods

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Constants

ACTION_MANAGE_HEALTH_PERMISSIONS

public static final String ACTION_MANAGE_HEALTH_PERMISSIONS

Activity action: Launch UI to manage (e.g. grant/revoke) health permissions.

Shows a list of apps which request at least one permission of the Health permission group.

Input: Intent.EXTRA_PACKAGE_NAME string extra with the name of the app requesting the action. Optional: Adding package name extras launches a UI to manager (e.g. grant/revoke) for this app.

Constant Value: "android.healthconnect.action.MANAGE_HEALTH_PERMISSIONS"

CATEGORY_HEALTH_PERMISSIONS

public static final String CATEGORY_HEALTH_PERMISSIONS

Used in conjunction with Intent.ACTION_VIEW_PERMISSION_USAGE to launch UI to show an app’s health permission rationale/data policy.

Note: Used by apps to define an intent filter in conjunction with Intent.ACTION_VIEW_PERMISSION_USAGE that the HC UI can link out to.

Constant Value: "android.intent.category.HEALTH_PERMISSIONS"

Public methods

aggregate

public void aggregate (AggregateRecordsRequest<T> request, 
                Executor executor, 
                OutcomeReceiver<AggregateRecordsResponse<T>, HealthConnectException> callback)

Get aggregations corresponding to request.

Parameters
request AggregateRecordsRequest: request for different aggregation. This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.
Returns
void This value cannot be null.

aggregateGroupByDuration

public void aggregateGroupByDuration (AggregateRecordsRequest<T> request, 
                Duration duration, 
                Executor executor, 
                OutcomeReceiver<List<AggregateRecordsGroupedByDurationResponse<T>>, HealthConnectException> callback)

Get aggregations corresponding to request. Use this API if results are to be grouped by concrete intervals of time, for example 5 Hrs, 10 Hrs etc.

Parameters
request AggregateRecordsRequest: request for different aggregation. This value cannot be null.
duration Duration: Duration on which to group by results This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
callback OutcomeReceiver: Callback to receive result of performing this operation.

TODO(b/251194265): User permission checks once available. This value cannot be null.

See also:

aggregateGroupByPeriod(AggregateRecordsRequest, Period, Executor, OutcomeReceiver>, HealthConnectException>)

aggregateGroupByPeriod

public void aggregateGroupByPeriod (AggregateRecordsRequest<T> request, 
                Period period, 
                Executor executor, 
                OutcomeReceiver<List<AggregateRecordsGroupedByPeriodResponse<T>>, HealthConnectException> callback)

Get aggregations corresponding to request. Use this API if results are to be grouped by number of days. This API handles changes in ZoneOffset when computing the data on a per-day basis.

Parameters
request AggregateRecordsRequest: Request for different aggregation. This value cannot be null.
period Period: Period on which to group by results This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.

See also:

deleteRecords

public void deleteRecords (Class<? extends Record> recordType, 
                TimeRangeFilter timeRangeFilter, 
                Executor executor, 
                OutcomeReceiver<VoidHealthConnectException> callback)

Deletes records based on the TimeRangeFilter.

Deletions are performed in a transaction i.e. either all will be deleted or none

Parameters
recordType Class: recordType to perform delete operation on. This value cannot be null.
timeRangeFilter TimeRangeFilter: time filter based on which to delete the records. This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.

deleteRecords

public void deleteRecords (List<RecordIdFilter> recordIds, 
                Executor executor, 
                OutcomeReceiver<VoidHealthConnectException> callback)

Deletes records based on RecordIdFilter.

Deletions are performed in a transaction i.e. either all will be deleted or none

Parameters
recordIds List: recordIds on which to perform delete operation. This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.
Throws
IllegalArgumentException if recordIds is empty

getChangeLogToken

public void getChangeLogToken (ChangeLogTokenRequest request, 
                Executor executor, 
                OutcomeReceiver<StringHealthConnectException> callback)

Get token for {HealthConnectManager#getChangeLogs}. Changelogs requested corresponding to this token will be post the time this token was generated by the system all items that match the given filters.

Tokens from this request are to be passed to {HealthConnectManager#getChangeLogs}

Parameters
request ChangeLogTokenRequest: A request to get changelog token This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.

getChangeLogs

public void getChangeLogs (ChangeLogsRequest changeLogsRequest, 
                Executor executor, 
                OutcomeReceiver<ChangeLogsResponseHealthConnectException> callback)

Get change logs post the time when token was generated.

Parameters
changeLogsRequest ChangeLogsRequest: The token from HealthConnectManager#getChangeLogToken. This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.

insertRecords

public void insertRecords (List<Record> records, 
                Executor executor, 
                OutcomeReceiver<InsertRecordsResponseHealthConnectException> callback)

Inserts records into the HealthConnect database. The records returned in InsertRecordsResponse contains the unique IDs of the input records. The values are in same order as records. In case of an error or a permission failure the HealthConnect service, OutcomeReceiver#onError will be invoked with a HealthConnectException.

Parameters
records List: list of records to be inserted. This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
callback OutcomeReceiver: Callback to receive result of performing this operation.

TODO(b/251194265): User permission checks once available. This value cannot be null.

Throws
RuntimeException for internal errors

readRecords

public void readRecords (ReadRecordsRequestUsingIds<T> request, 
                Executor executor, 
                OutcomeReceiver<ReadRecordsResponse<T>, HealthConnectException> callback)

API to read records based on RecordIdFilter.

Parameters
request ReadRecordsRequestUsingIds: ReadRecordsRequestUsingIds request containing a list of RecordIdFilter and recordType to perform read operation. This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.

readRecords

public void readRecords (ReadRecordsRequestUsingFilters<T> request, 
                Executor executor, 
                OutcomeReceiver<ReadRecordsResponse<T>, HealthConnectException> callback)

API to read records based on ReadRecordsRequestUsingFilters.

Parameters
request ReadRecordsRequestUsingFilters: Read request based on ReadRecordsRequestUsingFilters This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.

updateRecords

public void updateRecords (List<Record> records, 
                Executor executor, 
                OutcomeReceiver<VoidHealthConnectException> callback)

Updates records into the HealthConnect database. In case of an error or a permission failure the HealthConnect service, OutcomeReceiver#onError will be invoked with a HealthConnectException.

In case the input record to be updated does not exist in the database or the caller is not the owner of the record then HealthConnectException#ERROR_INVALID_ARGUMENT will be thrown.

Parameters
records List: list of records to be updated. This value cannot be null.
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
callback OutcomeReceiver: Callback to receive result of performing this operation. This value cannot be null.
Throws
IllegalArgumentException if at least one of the records is missing both ClientRecordID and UUID.

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2023-02-08 UTC.