SessionsApi | Android Developers

Quicknav Quicknav
Links

- Google Play Developer Console
- Android Developers Blog
- About Android

Android Sites

- Android.com
- Android Developers
- Android Open Source Project

Language
close

- - About
  - Wear
  - TV
  - Auto
- - Get Started
  - Devices
  - Style
  - Patterns
  - Building Blocks
  - Downloads
  - Videos
- - Training
  - API Guides
  - Reference
  - Tools
  - Google Services
  - Samples
- - Google Play
  - Essentials
  - Get Users
  - Engage & Retain
  - Monetize
  - Analyze
  - Tools & Reference
  - Developer Stories

## Results

Loading...
public interface

# SessionsApi

com.google.android.gms.fitness.SessionsApi

## Class Overview

API 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(GoogleApiClient, Session)` and `stopSession(GoogleApiClient, String)` methods mark the time range of the session. Data inserted using the `HistoryApi` 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(GoogleApiClient, SessionInsertRequest)`. This is designed for bulk insertion when importing a user's historic data into Google Fit; for live sessions use the previous API. 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(GoogleApiClient, SessionReadRequest)`.

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

Deleting sessions is handled like any other data deletion, via `deleteData(GoogleApiClient, DataDeleteRequest)`.

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

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

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

         PendingResult<Status> pendingResult
             = Fitness.SessionsApi.startSession(client, session);

## Summary

Nested Classes
class | SessionsApi.ViewIntentBuilder | Builder of intents to view sessions stored in Google Fit.

Public Methods
abstract PendingResult<Status> | insertSession(GoogleApiClient client, SessionInsertRequest request) Inserts specified `Session` and corresponding data into the user's Google Fit store on behalf of the current application.
abstract PendingResult<SessionReadResult> | readSession(GoogleApiClient client, 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.
abstract PendingResult<Status> | registerForSessions(GoogleApiClient client, PendingIntent intent) Registers 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 identifier) Stops active sessions for the current application.
abstract PendingResult<Status> | unregisterForSessions(GoogleApiClient client, PendingIntent intent) Unregisters from session start and end notifications using a `PendingIntent`.

## Public Methods

#### public abstract PendingResult<Status> insertSession (GoogleApiClient client, 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

client | an existing GoogleApiClient. It does not need to be connected at the time of this call, but the insertion will be delayed until the connection is complete.
request | the `Session` and its associated data, which includes details of the session and data set for data source(s).

#### public abstract PendingResult<SessionReadResult> readSession (GoogleApiClient client, 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.

##### Parameters

client | an existing GoogleApiClient. It does not need to be connected at the time of this call, but the insertion will be delayed until the connection is complete.
request | a built request specifying the specific session, time interval and/or data type for the query

##### Returns

- a pending result containing the requested session and data. Includes a data set for each session and data type that was returned.

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

Registers 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:

- `EXTRA_START_TIME` set to the Session's start time.
- `EXTRA_END_TIME` optionally set to the Session's end time if the session has ended.

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)`.

##### Parameters

client | an existing GoogleApiClient. Must be connected at the time of this call.
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 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.
identifier | the identifier of the session to stop. If `null` is passed all active sessions will be stopped.

##### Returns

- a pending result containing the list of sessions that were stopped

##### Throws

IllegalStateException | if client is not connected

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

Unregisters from session start and end notifications using a `PendingIntent`.

##### Parameters

client | an existing GoogleApiClient. Must be connected at the time of this call.

##### Throws

IllegalStateException | if client is not connected

##### See Also

- `ERROR(#registerForSessions(GoogleApiClient, PendingIntent)}/{@link #registerForSessions(GoogleApiClient, PendingIntent)})`