SessionsClient

public class SessionsClient extends GoogleApi<FitnessOptions>

Client for creating and managing sessions of user activity in Google Fit. Sessions are a way of storing user-visible groups of related stream data in a useful and shareable manner, and allow for easy querying of the data in a detailed or aggregated fashion.

The startSession(Session) and stopSession(String) methods mark the time range of the session. Data inserted using the HistoryClient during the period where the session is in progress will be associated with that Session once it is stopped.

Another way of adding sessions is with insertSession(SessionInsertRequest). This is designed for bulk insertion when importing a user's historic data into Google Fit; for live sessions use the previous Client. All data in Google Fit that falls within the time range of an inserted session will be implicitly associated with that session.

Once a session has been saved, it can be retrieved using readSession(SessionReadRequest).

Clients can register to be notified of sessions, and remove existing registrations, using registerForSessions(PendingIntent) and unregisterForSessions(PendingIntent). Notifications will be sent for sessions when they are started and stopped.

Deleting sessions is handled like any other data deletion, via deleteData(com.google.android.gms.fitness.request.DataDeleteRequest).

The Sessions Client should be accessed via the Fitness entry point. Example:


    GoogleSignInAccount gsa = ...;

    Session session = new Session.Builder()
        .setName(sessionName)
        .setIdentifier(identifier)
        .setDescription(description)
        .setStartTime(startTime.getMillis(), TimeUnit.MILLISECONDS)
        .build();

    Task<Void> response = Fitness.getSessionsClient(this, gsa)
        .startSession(session);
 

Public Method Summary

Task<Void>
insertSession(SessionInsertRequest request)
Inserts specified Session and corresponding data into the user's Google Fit store on behalf of the current application.
Task<SessionReadResponse>
readSession(SessionReadRequest request)
Reads data from the user's Google Fit store of the specific type(s) and for the specific session(s) selected from the request parameters.
Task<Void>
registerForSessions(PendingIntent intent)
Registers for notifications of session start and end events using a PendingIntent.
Task<Void>
startSession(Session session)
Starts a new active session for the current application.
Task<List<Session>>
stopSession(String identifier)
Stops active sessions for the current application.
Task<Void>
unregisterForSessions(PendingIntent intent)
Unregisters from session start and end notifications using a PendingIntent.

Inherited Method Summary

Public Methods

public Task<Void> insertSession (SessionInsertRequest request)

Inserts specified Session and corresponding data into the user's Google Fit store on behalf of the current application. Useful for bulk upload of a previously recorded session.

Parameters
request the Session and its associated data, which includes details of the session and data set for data source(s).

public Task<SessionReadResponse> readSession (SessionReadRequest request)

Reads data from the user's Google Fit store of the specific type(s) and for the specific session(s) selected from the request parameters.

Note that, by default, only sessions from your app are returned. To read sessions from other apps, readSessionsFromAllApps() can be used.

Parameters
request a built request specifying the specific session, time interval and/or data type for the query
Returns
  • a task containing the requested session and data. Includes a data set for each session and data type that was returned.

public Task<Void> registerForSessions (PendingIntent intent)

Registers for notifications of session start and end events using a PendingIntent. Notifications will be sent only for sessions that are explicitly started by an app via startSession(Session).

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_TYPE_PREFIX followed by the name of the activity associated with this session, and can be computed by getMimeType(String).

Any previously registrations that have the same PendingIntent (as defined by equals(Object)) will be replaced by this registration.

Parameters
intent a callback intent to be sent every time a session is started or stopped.

public Task<Void> startSession (Session session)

Starts a new active session for the current application.

Parameters
session the session to be started
Returns
  • a task containing the status of request
Throws
IllegalStateException if client is not connected

public Task<List<Session>> stopSession (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
identifier the identifier of the session to stop. If null is passed all active sessions will be stopped.
Returns
  • a task containing the list of sessions that were stopped
Throws
IllegalStateException if client is not connected

public Task<Void> unregisterForSessions (PendingIntent intent)

Unregisters from session start and end notifications using a PendingIntent.

Parameters
intent the PendingIntent that was used in the registerForSessions call or is equal as defined by equals(Object).
Throws
IllegalStateException if client is not connected