Working with the Fitness History

The History API enables your app to perform bulk operations on the fitness store: reading, inserting, and deleting fitness data. Use the History API to:

  • Import batch data into Google Fit.
  • Read fitness data that was inserted or recorded using other apps.
  • 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 data

Read detailed and aggregate data

To read data from the fitness history, first create a DataReadRequest instance:

The data request can specify multiple data types to return, effectively combining multiple data queries into one call. This example only specifies the TYPE_STEP_COUNT_DELTA 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 each 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.

After you create a DataReadRequest instance, use the HistoryApi.readData() method and wait synchronously or provide a callback method to process the data from the fitness history.

The following example demonstrates how to obtain the DataPoint instances from a DataSet:

Read daily total data

Google Fit also provides simple access to the daily total of a specified data type. Use the HistoryApi.readDailyTotal() 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 TYPE_STEP_COUNT_DELTA 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 AGGREGATE_INPUT_TYPES.

Google Fit does not require authorization to subscribe to TYPE_STEP_COUNT_DELTA updates from the HistoryApi.readDailyTotal() 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 watch faces).

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.

Insert data

To insert data into the fitness history, first create a DataSet instance:

After you create a DataSet instance, use the HistoryApi.insertData method and wait synchronously or provide a callback method to check the status of the insertion.

Manage conflicting data points

Each DataPoint in a your app's DataSet must have a startTime and an endTime that defines a unique interval within that DataSet, with no overlap between DataPoint instances.

If your app attepts to insert a new DataPoint that conflicts with existing DataPoint instances, this conflict is resolved by the new DataPoint being discarded. To replace a DataPoint, first delete any DataPoint instances with an endTime after the startTime of the new DataPoint, or with a startTime before the endTime of the new DataPoint.

Delete data

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 HistoryApi.deleteData method:

Apps can provide specific sessions or delete all data. Fore more information, see the API reference for DataDeleteRequest.

Send feedback about...

Google Fit
Google Fit