Android APIs
public interface

RecordingApi

com.google.android.gms.fitness.RecordingApi

Class Overview

The Recording API allows applications to request background collection of sensor data by creating subscriptions. Subscriptions are active when the requesting app is not running, and persisted through system restarts. Collected data can be queried using the History API.

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

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

     GoogleApiClient client = new GoogleApiClient.Builder(context)
         .addApi(Fitness.API)
         ...
         .build();
     client.connect();

     // Samples the user's activity once per minute.
     PendingResult<Status> pendingResult = Fitness.RecordingApi.subscribe(
         client, DataTypes.ACTIVITY_SAMPLE);
 
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.

Summary

Public Methods
abstract PendingResult<ListSubscriptionsResult> listSubscriptions(GoogleApiClient client, DataType dataType)
Reads all existing subscriptions for the current application.
abstract PendingResult<ListSubscriptionsResult> listSubscriptions(GoogleApiClient client)
Reads all existing subscriptions for the current application.
abstract PendingResult<Status> registerForSessions(GoogleApiClient client, PendingIntent intent)
Register for notifications of session start and end events using a PendingIntent.
abstract PendingResult<Status> startSession(GoogleApiClient client, Session session)
Starts a new active session for the current application.
abstract PendingResult<SessionStopResult> stopSession(GoogleApiClient client, String name, String identifier)
Stops active sessions for the current application.
abstract PendingResult<Status> subscribe(GoogleApiClient client, DataType dataType)
Subscribe to background collection of data of a specific type on behalf of the current application.
abstract PendingResult<Status> subscribe(GoogleApiClient client, DataSource dataSource)
Subscribe to background collection of data from a specific source on behalf of the current application.
abstract PendingResult<Status> unregisterForSessions(GoogleApiClient client, PendingIntent intent)
Unregister for notifications of session start & end events using a PendingIntent.
abstract PendingResult<Status> unsubscribe(GoogleApiClient client, DataType dataType)
Unsubscribes from background data collection for the current application.

Public Methods

public abstract PendingResult<ListSubscriptionsResult> listSubscriptions (GoogleApiClient client, DataType dataType)

Reads all existing subscriptions for the current application.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the list operation will be delayed until the connection is complete.
dataType list only subscriptions to this data type
Returns
  • a pending result containing the found subscriptions.

public abstract PendingResult<ListSubscriptionsResult> listSubscriptions (GoogleApiClient client)

Reads all existing subscriptions for the current application.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the list operation will be delayed until the connection is complete.
Returns
  • a pending result containing the found subscriptions.

public abstract PendingResult<Status> registerForSessions (GoogleApiClient client, PendingIntent intent)

Register for notifications of session start and end events using a PendingIntent.

The application specifies a PendingIntent callback (typically an IntentService) which will be called when sessions are started or stopped. When the PendingIntent is called, the application can use extract(android.content.Intent) to extract the Session from the intent. In addition, the following Intent extras can be used:

Finally, the Intent's type will be set to MIME_ACTIVITY_TYPE_PREFIX/<activity_type>, where activity_type is the name of the activity associated with this session. See getActivityMimeType(int).

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the registration won't be added until the connection is complete.
intent a callback intent to be sent every time a session is started or stopped.

public abstract PendingResult<Status> startSession (GoogleApiClient client, Session session)

Starts a new active session for the current application.

Parameters
client an existing GoogleApiClient. Must be connected at the time of this call.
session the session to be started
Returns
  • a pending result containing the status of request
Throws
IllegalStateException if client is not connected

public abstract PendingResult<SessionStopResult> stopSession (GoogleApiClient client, String name, String identifier)

Stops active sessions for the current application. The active sessions to be stopped are chosen as per the specifications in the input request.

Parameters
client an existing GoogleApiClient. Must be connected at the time of this call.
name the name of the session to stop.
identifier the identifier of the session to stop.
Returns
  • a pending result containing the list of sessions that were stopped
Throws
IllegalStateException if client is not connected

public abstract PendingResult<Status> subscribe (GoogleApiClient client, 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 will be 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 Api.

If after the subscription is added the data source becomes unavailable (it may be disconnected or disabled, for instance), the subscription remains active. Once the data source becomes available again, new data points (and any data points that were batched while disconnected) are inserted.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the subscribe operation will be delayed until the connection is complete.
dataType the data type to subscribe to.

public abstract PendingResult<Status> subscribe (GoogleApiClient client, 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 Api.

If after the subscription is added the data source becomes unavailable (it may be disconnected or disabled, for instance), the subscription remains active. Once the data source becomes available again, new data points (and any data points that were batched while disconnected) are inserted.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the subscribe operation will be delayed until the connection is complete.
dataSource the data source to subscribe to.

public abstract PendingResult<Status> unregisterForSessions (GoogleApiClient client, PendingIntent intent)

Unregister for notifications of session start & end events using a PendingIntent.

Parameters
client an existing GoogleApiClient. Must be connected at the time of this call.
Throws
IllegalStateException if client is not connected

public abstract PendingResult<Status> unsubscribe (GoogleApiClient client, DataType dataType)

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

Parameters
client an existing GoogleApiClient. Must be connected at the time of this call.
dataType the data type to remove all subscriptions to.
Throws
IllegalStateException if client is not connected