com.google.android.gms.fitness.HistoryApi |
API for inserting, deleting, and reading data in Google Fit.
The readData(GoogleApiClient, DataReadRequest)
method should be used whenever historical
data is needed. It can be combined with a subscription in the Recording Api
to collect data in the background and query it later for displaying.
The insertData(GoogleApiClient, DataSet)
method can be used for batch insertion of
data that was collected outside of Google Fit. It can be useful when data is entered directly
by the user or imported from a device that isn't supported by the platform.
The History API should be accessed via the Fitness
entry point. Example:
GoogleApiClient client = new GoogleApiClient.Builder(context) .addApi(Fitness.HISTORY_API) ... .build(); client.connect(); PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi.readData( client, new DataReadRequest.Builder() .read(DataType.TYPE_STEP_COUNT_DELTA) .setTimeRange(startTime.getMillis(), endTime.getMillis(), TimeUnit.MILLISECONDS) .build()); DataReadResult readDataResult = pendingResult.await(); DataSet dataSet = readDataResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
HistoryApi.ViewIntentBuilder | Builder of intents to view data stored in Google Fit. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Deletes data from the user’s Google Fit history.
| |||||||||||
Inserts data collected from a data source directly into the user’s Google Fit history,
on behalf of the current application.
| |||||||||||
Reads the current daily total for the given
dataType . | |||||||||||
Reads the current daily total for the given
dataType from the local device only. | |||||||||||
Reads data from the user’s Google Fit history.
| |||||||||||
Adds a
PendingIntent listener to Google fitness store. | |||||||||||
Removes
PendingIntent , previously registered for receiving data update notifications,
from Google Fitness Store. | |||||||||||
Updates data collected from a data source directly into the user’s Google Fit history,
on behalf of the current application.
|
Deletes data from the user’s Google Fit history. This request will fail if the requesting app tries to delete data that it hasn't inserted. The user can delete data from any application (or hardware sensors) by visiting the Google Fit dashboard.
Parameters | |
---|---|
client |
GoogleApiClient :
an existing GoogleApiClient. It does not need to be connected at the time of
this call, but the deletion will be delayed until the connection is complete. |
request |
DataDeleteRequest :
specifying the data source/type and time range to delete |
Returns | |
---|---|
PendingResult<Status> |
a pending result containing status of the request |
Inserts data collected from a data source directly into the user’s Google Fit history, on behalf of the current application. Useful when the data source isn't compatible with Google Fit or for importing historical data.
If the data source can be exposed via a
BLE GATT profile,
an application-exposed sensor
,
or some other method compatible with Google Fit, it's preferable to create a subscription via
the Recording API instead of inserting data directly.
Parameters | |
---|---|
client |
GoogleApiClient :
an existing GoogleApiClient. It does not need to be connected at the time of
this call, but the insertion will be delayed until the connection is complete. |
dataSet |
DataSet :
the data we’re adding |
Returns | |
---|---|
PendingResult<Status> |
a pending result containing the status of the request |
Reads the current daily total for the given dataType
. The daily total will be
computed from midnight of the current day on the device's current timezone. The method
can be used as follows:
PendingResult<DailyTotalResult> result = HistoryApi.readDailyTotalFromLocalDevice(client, TYPE_STEP_COUNT_DELTA); DailyTotalResult totalResult = result.await(30, SECONDS); if (totalResult.getStatus().isSuccess()) { DataSet totalSet = totalResult.getTotal(); long total = totalSet.isEmpty() ? 0 : totalSet.getDataPoints().get(0).getValue(FIELD_STEPS).asInt(); } else { // handle failure }
This is a simplified version of
readData()
. When the requested data
type is TYPE_STEP_COUNT_DELTA
, authentication is not required to call this
method, making it specially suited for use by Watchface and Widget activities that don't
have the ability to show an authentication panel to the user.
This method is equivalent to:
readData(client, new DataReadRequest.Builder() .setTimeRange(midnight.getMillis(), now.getMillis(), TimeUnit.MILLISECONDS) .bucketByTime(1, TimeUnit.DAYS) .aggregate(DataTypes.STEP_COUNT_DELTA, AggregateDataTypes.STEP_COUNT_DELTA) .build());
This method aggregates data across different user devices, for instance a watch and
a phone. To aggregate data from the local device only,
see readDailyTotalFromLocalDevice(GoogleApiClient, DataType)
Parameters | |
---|---|
client |
GoogleApiClient
|
dataType |
DataType
|
Returns | |
---|---|
PendingResult<DailyTotalResult> |
a pending result containing the requested data. The pending result will contain a single DataSet. If no data has been collected for the requested data type today, the DataSet will be empty. |
Throws | |
---|---|
SecurityException |
if a required permission is missing for the requested
DataType .
|
Reads the current daily total for the given dataType
from the local device only.
The daily total will be computed from midnight of the current day on the device's current
timezone.
This method aggregates local data only. To aggregate data across different devices, see
readDailyTotal(GoogleApiClient, DataType)
Parameters | |
---|---|
client |
GoogleApiClient
|
dataType |
DataType
|
Returns | |
---|---|
PendingResult<DailyTotalResult> |
a pending result containing the requested data. The pending result will contain a single DataSet. If no data has been collected for the requested data type today, the DataSet will be empty. |
Throws | |
---|---|
SecurityException |
if a required permission is missing for the requested
DataType .
|
Reads data from the user’s Google Fit history. Values can be read in detailed or in aggregate formats. Aggregate data is presented in buckets, while detailed data is returned as a single data set.
Parameters | |
---|---|
client |
GoogleApiClient :
an existing GoogleApiClient. It does not need to be connected at the time of
this call, but the read operation will be delayed until the connection is complete. |
request |
DataReadRequest :
a built request specifying the data sources we’re interested in reading and
the time range of the returned data. |
Returns | |
---|---|
PendingResult<DataReadResult> |
a pending result containing the requested data. Includes a data set for each requested data source. |
Throws | |
---|---|
SecurityException |
if a required permission is missing for one or more of the
requested DataTypes or matching DataSources .
|
Adds a PendingIntent
listener to Google fitness store. This method can be called to
listen on updates in Google Fitness Store based on the request parameters.
For instance, a listener may be registered to the com.google.weight data type to receive
updates whenever new weight entries are added.
Once the register request succeeds, notification for updates in the Fitness Store are
delivered to the pendingIntent in the request. Historic data is not delivered, but can be
queried via the History API
. When the application is closing, or once data
update notifications are no longer needed, the listener should be removed.
The application specifies a PendingIntent callback (typically an IntentService), wrapped in
DataUpdateListenerRegistrationRequest
, which will be called when updates happen in
the Fitness Store. When the PendingIntent is called, the application can use
getDataUpdateNotification(android.content.Intent)
to extract the DataUpdateNotification from the intent. See the documentation of
PendingIntent
for more details.
Any previously registered requests that have the same PendingIntent
(as defined by equals(Object)
) will be replaced by this request.
Currently the API allows registering listeners using dataType
and
dataSource
for following data types:
TYPE_BASAL_METABOLIC_RATE
TYPE_BODY_FAT_PERCENTAGE
TYPE_HEIGHT
TYPE_NUTRITION
TYPE_WEIGHT
TYPE_WORKOUT_EXERCISE
Parameters | |
---|---|
client |
GoogleApiClient :
an existing GoogleApiClient. It does not need to be connected at the time of
this call, but the register operation will be delayed until the connection is complete |
request |
DataUpdateListenerRegistrationRequest :
request containing the parameters to identify the data updates which should
trigger notification to the pendingIntent |
Returns | |
---|---|
PendingResult<Status> |
Throws | |
---|---|
SecurityException |
if a required permission is missing for the requested
DataType or DataSource .
|
Removes PendingIntent
, previously registered for receiving data update notifications,
from Google Fitness Store. Should be called whenever data update notifications are no longer
needed.
Parameters | |
---|---|
client |
GoogleApiClient :
an existing GoogleApiClient. Must be connected at the time of this call. |
pendingIntent |
PendingIntent :
the PendingIntent that was used in the
registerDataUpdateListener(GoogleApiClient, DataUpdateListenerRegistrationRequest) request or is equal as defined by
equals(Object) . |
Returns | |
---|---|
PendingResult<Status> |
Throws | |
---|---|
IllegalStateException |
if client is not connected |
Updates data collected from a data source directly into the user’s Google Fit history, on behalf of the current application. For the provided time range, data is deleted for the given data source and new data is added to the system.
Useful when the data source isn't compatible with Google Fit or for importing historical data.
Parameters | |
---|---|
client |
GoogleApiClient :
an existing GoogleApiClient. It does not need to be connected at the time of
this call, but the update will be delayed until the connection is complete. |
request |
DataUpdateRequest :
specifying the time range to update and data |
Returns | |
---|---|
PendingResult<Status> |
a pending result containing the status of the request |