The History API enables your app to perform bulk operations on the fitness store: reading, inserting, updating, and deleting fitness data. Use the History API to:
- Read fitness data that was inserted or recorded using other apps.
- Import batch data into Google Fit.
- Update data in Google Fit.
- Delete data that your app previously stored in the fitness history.
- Disable Google Fit for an app.
To insert fitness data with session metadata, you can use the Sessions API.
Read detailed and aggregate data
To read data from the fitness history, first create a
The data request can specify multiple data types to return, effectively
combining multiple data queries into one call. This example only specifies the
data type. The data request can also indicate whether to return time-series data
points or aggregated data points. This example uses aggregated data points where
DataPoint represents the number of steps walked in a day. For this
particular use case, aggregated data points have two advantages:
- Your app and the fitness store exchange smaller amounts of data.
- Your app does not have to aggregate the data manually.
Your app can use data requests to retrieve lots of different types of data. The
following example shows how to create a
to get calories burned for each activity performed within the specified time
range. The resulting data matches the calories per activity as reported in the
Google Fit app, with each activity getting its own bucket of calorie data.
DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED) .bucketByActivityType(1, TimeUnit.SECONDS) .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS) .build();
The following example demonstrates how to obtain the
DataPoint instances from
Read daily total data
Google Fit also provides simple access to the daily total of a
specified data type. Use the
method to retrieve the data type that you specify as of midnight of the current
day in the device's current timezone. For example, pass in the
data type to this method to retrieve the daily total steps. You may pass in
an instantaneous data type
that has an aggregate daily total. For more information on the
supported data types, see
Google Fit does not require authorization to subscribe to
updates from the
method when this method is called using the default account and no scopes are
specified. This can be useful if you require step data for use in areas where
you are unable to show the permissions panel (for example, Android Wear
Users prefer to see consistent step counts across the Google Fit app, other fitness apps, and Android Wear watch faces, as this provides them with a consistent and reliable experience. To keep step counts consistent, subscribe to steps in the Google Fit platform from your fitness app or watch face, and then call this method every 30 seconds in interactive mode, and every 60 seconds in ambient mode. For more information on how to use this data in a watch face, see Showing Information in Watch Faces and the Android Watch Face sample application.
To insert data into the fitness history, first create a
Manage conflicting data points
If your app attepts to insert a new
DataPoint that conflicts with an existing
DataPoint instance, the new
DataPoint is discarded. To insert a new
DataPoint that may overlap existing data points,
method described in Update data.
Google Fit enables your app to update fitness data that it previously inserted
into the fitness history. To add data to the fitness history for a new
DataSet, or to add new
DataPoint instances that do not conflict with
existing data points as described in Manage conflicting data points,
your app should use the
To update fitness history, use the
HistoryApi.updateData method. This
method deletes any existing
DataPoint instances that overlap with
instances added using this method.
To update fitness history data, first create a
DataUpdateRequest.Builder() to create a new data update request,
and use the
HistoryApi.updateData method to make the request.
Google Fit enables your app to delete fitness data that it inserted into the fitness history.
To delete fitness data from the fitness history, use the
Apps can provide specific sessions or delete all data. For more information,
see the API reference for