Session

public class Session extends Object
implements Parcelable

A Session represents a time interval with associated metadata. Sessions provide a mechanism to store user-visible groups of related stream data in a useful and shareable manner, and allows for easy querying of the data in a detailed or aggregated fashion. The start and end times for sessions will be controlled by applications, and can be used to represent user-friendly groupings of activities, such as "bike ride", "marathon training run". Any data in Google Fit which falls within this time range is implicitly associated with the session.

A session consists of the following fields:

  • startTime: the timestamp when the session started. This is a mandatory field.
  • endTimeMillis: the timestamp when the session ended. If not specified, the session is considered to be still ongoing. If specified, the end time should be strictly later than the start time. This is an optional field.
  • name: a human readable name, possibly specified by the user. For instance, "Sunday bike ride"
  • identifier: a unique identifier for the session. Should be unique for the given application and user. The application can use this identifier to later fetch a particular session. If the identifier is not specified, one will be created from the start time and name.
  • description: description of the session. Can be filled in by the user with specific notes for the session. This is an optional field.
  • activity: the activity associated with this session; for instance, biking. Can be used to identify sessions that encompass a single user activity. This is an optional field.
  • packageName: the package name of the application that added this session. This is inferred automatically by the Fitness Platform.

Example usage:

     new Session.Builder()
          .setName(sessionName)
          .setIdentifier(identifier)
          .setDescription(description)
          .setStartTime(startTime, TimeUnit.MILLISECONDS)
          .setEndTime(endTime, TimeUnit.MILLISECONDS)
          .setActivity(FitnessActivities.BIKING)
          .build();
 
See Also

Nested Class Summary

class Session.Builder Builder used to create new Sessions. 

Constant Summary

String EXTRA_SESSION Name for the parcelable intent extra containing a session.
String MIME_TYPE_PREFIX The common prefix for session MIME types.

Inherited Constant Summary

Public Method Summary

boolean
equals(Object other)
static Session
extract(Intent intent)
Extracts the session extra from the given intent, such as a callback intent received after registering to session start/end notifications, or an intent to view a session.
long
getActiveTime(TimeUnit timeUnit)
Returns the active time period of the session.
String
getActivity()
Returns the activity associated with this session, if set.
String
getAppPackageName()
Returns the package name for the application responsible for adding the session.
String
getDescription()
Returns the description for this session.
long
getEndTime(TimeUnit timeUnit)
Returns the end time for the session, in the given unit since epoch.
String
getIdentifier()
Returns the identifier for this session.
static String
getMimeType(String activity)
Returns the MIME type which describes a Session for a particular activity.
String
getName()
Returns the name for this session, if set.
long
getStartTime(TimeUnit timeUnit)
Returns the start time for the session, in the given time unit since epoch.
boolean
hasActiveTime()
Returns whether the session active time is set.
int
boolean
isOngoing()
Returns whether the session is ongoing.
String
void
writeToParcel(Parcel dest, int flags)

Inherited Method Summary

Constants

public static final String EXTRA_SESSION

Name for the parcelable intent extra containing a session. It can be extracted using extract(Intent).

Constant Value: "vnd.google.fitness.session"

public static final String MIME_TYPE_PREFIX

The common prefix for session MIME types. The MIME type for a particular session will be this prefix followed by the session's activity name. Examples:

 vnd.google.fitness.session/running
 vnd.google.fitness.session/volleyball.beach
 
The session's activity type is returned by getActivity(). The MIME type can be computed from the activity using getMimeType(String)
Constant Value: "vnd.google.fitness.session/"

Public Methods

public boolean equals (Object other)

public static Session extract (Intent intent)

Extracts the session extra from the given intent, such as a callback intent received after registering to session start/end notifications, or an intent to view a session.

Returns
  • The extracted Session, or null if the given intent does not contain a Session.

public long getActiveTime (TimeUnit timeUnit)

Returns the active time period of the session.

Make sure to use hasActiveTime() before using this method.

public String getActivity ()

Returns the activity associated with this session, if set. Else returns FitnessActivities.UNKNOWN.

public String getAppPackageName ()

Returns the package name for the application responsible for adding the session. or null if unset/unknown. The PackageManager can be used to query relevant data on the application, such as the name, icon, or logo.

public String getDescription ()

Returns the description for this session.

public long getEndTime (TimeUnit timeUnit)

Returns the end time for the session, in the given unit since epoch. If the session is ongoing (it hasn't ended yet), this will return 0.

public String getIdentifier ()

Returns the identifier for this session.

public static String getMimeType (String activity)

Returns the MIME type which describes a Session for a particular activity. The MIME type is used in intents such as the session view intent.

Parameters
activity One of the activities in FitnessActivities.

public String getName ()

Returns the name for this session, if set.

public long getStartTime (TimeUnit timeUnit)

Returns the start time for the session, in the given time unit since epoch. A valid start time is always set.

public boolean hasActiveTime ()

Returns whether the session active time is set.

public int hashCode ()

public boolean isOngoing ()

Returns whether the session is ongoing. If the session has ended, this will return false.

public String toString ()

public void writeToParcel (Parcel dest, int flags)