RecordingClient

public class RecordingClient extends GoogleApi<Api.ApiOptions.HasGoogleSignInAccountOptions>

Client which enables low-power, always-on background collection of sensor data into the Google Fit store. Sensor subscriptions are active when the requesting app is not running, and persisted through system restarts. Collected data can be queried using the History Client.

Unlike the Sensors Client, the Recording Client does not support delivery of live sensor events. When live data is needed (such as when the app is open in the foreground), a listener should be added with the Sensors Client. An app can have both an active subscription and an active listener at the same time. Unlike listeners, subscriptions don't need to be removed and re-added periodically.

The Recording Client should be accessed from the Fitness entry point. Example:


    GoogleSignInOptionsExtension fitnessOptions =
        FitnessOptions.builder()
            .addDataType(DataType.TYPE_ACTIVITY_SAMPLES, FitnessOptions.ACCESS_READ)
            .build();

    GoogleSignInAccount googleSignInAccount =
        GoogleSignIn.getAccountForExtension(this, fitnessOptions);

    // Samples the user's activity once per minute.
    Task<Void> response = Fitness.getRecordingClient(this, googleSignInAccount)
        .subscribe(DataType.TYPE_ACTIVITY_SAMPLES);
 
Creating a subscription for a particular data type requires user permission for access to data of that type, and permissions can be revoked by the user through Google Play Services settings.

Public Method Summary

Task<List<Subscription>>
listSubscriptions()
Reads all existing subscriptions for the current application.
Task<List<Subscription>>
listSubscriptions(DataType dataType)
Reads all existing subscriptions for the current application to a particular data type.
Task<Void>
subscribe(DataSource dataSource)
Subscribe to background collection of data from a specific source on behalf of the current application.
Task<Void>
subscribe(DataType dataType)
Subscribe to background collection of data of a specific type on behalf of the current application.
Task<Void>
unsubscribe(Subscription subscription)
Unsubscribes from background data collection for the current application.
Task<Void>
unsubscribe(DataType dataType)
Unsubscribes from background data collection for the current application for a particular data type.
Task<Void>
unsubscribe(DataSource dataSource)
Unsubscribes from background data collection for the current application for a particular data source.

Inherited Method Summary

Public Methods

public Task<List<Subscription>> listSubscriptions ()

Reads all existing subscriptions for the current application.

Returns
  • A task containing the found subscriptions.

public Task<List<Subscription>> listSubscriptions (DataType dataType)

Reads all existing subscriptions for the current application to a particular data type.

Parameters
dataType List only subscriptions to this data type.
Returns
  • A task containing the found subscriptions.

public Task<Void> subscribe (DataSource dataSource)

Subscribe to background collection of data from a specific source on behalf of the current application.

After the subscription is successfully added, new data points in the requested data stream are persisted to the Google Fit store, and can be queried via the History Client.

If after the subscription is added the data source becomes unavailable (it may be disconnected or disabled, for instance), the subscription remains active, and recording resumes once the data source becomes available again. Data points from the time interval when the data source was unavailable may or may not be recorded, depending on whether batching is supported.

Note that data sources won't always be available (for example, external devices could go offline). If data sources for the subscription aren't available, the request will still (silently) succeed. Then, once the relevant data sources become available, the data will start recording.

If a subscription with matching DataType or DataSource already exists, the existing subscription is updated with the recording rate (and any other parameters) from this request.

Subscribing to a DataSource which derives from data collected using activity recognition requires the ACTIVITY_RECOGNITION runtime permission if your app is targeting API level 29. If the permission is lost during recording, the registration will be removed.

Data types which trigger activity collection include:

Parameters
dataSource The data source to subscribe to.
Throws
SecurityException If a required permission is missing for the requested DataSource.

public Task<Void> subscribe (DataType dataType)

Subscribe to background collection of data of a specific type on behalf of the current application.

Specifying a data type instead of a data source means that the default data source is used to serve this request. The default is based on the data type and the available data sources. The default data source could be a combined data source, and its data might be averaged or filtered. As new data sources for the data type become available, the default data source might change.

After the subscription is successfully added, new data points in the requested data stream are persisted to the Google Fit store, and can be queried via the History Client.

If after the subscription is added the data source becomes unavailable (it may be disconnected or disabled, for instance), the subscription remains active, and recording resumes once the data source becomes available again. Data points from the time interval when the data source was unavailable may or may not be recorded, depending on whether batching is supported.

Note that data sources won't always be available (for example, external devices could go offline). If data sources for the subscription aren't available, the request will still (silently) succeed. Then, once the relevant data sources become available, the data will start recording.

If the requested subscription already exists, the request is a no-op.

Subscribing to a DataType which derives from data collected using activity recognition requires the ACTIVITY_RECOGNITION runtime permission if your app is targeting API level 29. If the permission is lost during recording, the registration will be removed.

Data types which trigger activity collection include:

Parameters
dataType The data type to subscribe to.
Throws
SecurityException If a required permission is missing for the requested DataType.

public Task<Void> unsubscribe (Subscription subscription)

Unsubscribes from background data collection for the current application. Should be called when the application no longer wishes to record data.

Removing a Subscription that does not already exist is a no-op.

Parameters
subscription The subscription to remove.

public Task<Void> unsubscribe (DataType dataType)

Unsubscribes from background data collection for the current application for a particular data type. Should be called when the application no longer wishes to record data.

If subscriptions have previously been made for a data source, these subscriptions are removed if the DataType matches.

Unsubscribing from a DataType for which a subscription does not already exist is a no-op.

Parameters
dataType The data type to remove. All subscriptions to the particular data type (or data sources with this type) are removed

public Task<Void> unsubscribe (DataSource dataSource)

Unsubscribes from background data collection for the current application for a particular data source. Should be called when the application no longer wishes to record data.

Unsubscribing from a DataSource for which a subscription does not already exist is a no-op.

Parameters
dataSource The data source to unsubscribe from.