# Session

public class **Session** extends [Object](https://developer.android.com/reference/java/lang/Object.html)  
implements [Parcelable](https://developer.android.com/reference/android/os/Parcelable.html) 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

- `https://developers.google.com/android/reference/com/google/android/gms/fitness/SessionsClient`

### Nested Class Summary

|---|---|---|---|
| class | [Session.Builder](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session.Builder) || Builder used to create new Sessions. |

### Constant Summary

|---|---|---|
| [String](https://developer.android.com/reference/java/lang/String.html) | [EXTRA_SESSION](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#EXTRA_SESSION) | Name for the parcelable intent extra containing a session. |
| [String](https://developer.android.com/reference/java/lang/String.html) | [MIME_TYPE_PREFIX](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#MIME_TYPE_PREFIX) | The common prefix for session MIME types. |

### Inherited Constant Summary

From interface android.os.Parcelable

|---|---|---|
| int | CONTENTS_FILE_DESCRIPTOR |   |
| int | PARCELABLE_WRITE_RETURN_VALUE |   |

### Public Method Summary

|---|---|
| boolean | [equals](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#equals(java.lang.Object))([Object](https://developer.android.com/reference/java/lang/Object.html) other) |
| static [Session](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session) | [extract](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#extract(android.content.Intent))([Intent](https://developer.android.com/reference/android/content/Intent.html) intent) Extracts the session extra from the given intent, such as a callback intent received after `https://developers.google.com/android/reference/com/google/android/gms/fitness/SessionsClient#registerForSessions(android.app.PendingIntent)` to session start/end notifications, or an intent to `https://developers.google.com/android/reference/com/google/android/gms/fitness/SessionsApi.ViewIntentBuilder`. |
| long | [getActiveTime](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getActiveTime(java.util.concurrent.TimeUnit))([TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit.html) timeUnit) Returns the active time period of the session. |
| [String](https://developer.android.com/reference/java/lang/String.html) | [getActivity](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getActivity())() Returns the activity associated with this session, if set. |
| [String](https://developer.android.com/reference/java/lang/String.html) | [getAppPackageName](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getAppPackageName())() Returns the package name for the application responsible for adding the session. |
| [String](https://developer.android.com/reference/java/lang/String.html) | [getDescription](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getDescription())() Returns the description for this session. |
| long | [getEndTime](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getEndTime(java.util.concurrent.TimeUnit))([TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit.html) timeUnit) Returns the end time for the session, in the given unit since epoch. |
| [String](https://developer.android.com/reference/java/lang/String.html) | [getIdentifier](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getIdentifier())() Returns the identifier for this session. |
| static [String](https://developer.android.com/reference/java/lang/String.html) | [getMimeType](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getMimeType(java.lang.String))([String](https://developer.android.com/reference/java/lang/String.html) activity) Returns the MIME type which describes a Session for a particular activity. |
| [String](https://developer.android.com/reference/java/lang/String.html) | [getName](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getName())() Returns the name for this session, if set. |
| long | [getStartTime](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getStartTime(java.util.concurrent.TimeUnit))([TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit.html) timeUnit) Returns the start time for the session, in the given time unit since epoch. |
| boolean | [hasActiveTime](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#hasActiveTime())() Returns whether the session active time is set. |
| int | [hashCode](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#hashCode())() |
| boolean | [isOngoing](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#isOngoing())() Returns whether the session is ongoing. |
| [String](https://developer.android.com/reference/java/lang/String.html) | [toString](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#toString())() |
| void | [writeToParcel](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#writeToParcel(android.os.Parcel,%20int))([Parcel](https://developer.android.com/reference/android/os/Parcel.html) dest, int flags) |

### Inherited Method Summary

From class java.lang.Object

|---|---|
| [Object](https://developer.android.com/reference/java/lang/Object.html) | clone() |
| boolean | equals([Object](https://developer.android.com/reference/java/lang/Object.html) arg0) |
| void | finalize() |
| final [Class](https://developer.android.com/reference/java/lang/Class.html)\<?\> | getClass() |
| int | hashCode() |
| final void | notify() |
| final void | notifyAll() |
| [String](https://developer.android.com/reference/java/lang/String.html) | toString() |
| final void | wait(long arg0, int arg1) |
| final void | wait(long arg0) |
| final void | wait() |

From interface android.os.Parcelable

|---|---|
| abstract int | describeContents() |
| abstract void | writeToParcel([Parcel](https://developer.android.com/reference/android/os/Parcel.html) arg0, int arg1) |

## Constants

#### public static final [String](https://developer.android.com/reference/java/lang/String.html)
**EXTRA_SESSION**

Name for the parcelable intent extra containing a session. It can be extracted using
`https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#extract(android.content.Intent)`.
Constant Value: "vnd.google.fitness.session"

#### public static final [String](https://developer.android.com/reference/java/lang/String.html)
**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 `https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getActivity()`. The MIME type can be computed from the activity using `https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#getMimeType(java.lang.String)` Constant Value: "vnd.google.fitness.session/"

## Public Methods

#### public boolean **equals** ([Object](https://developer.android.com/reference/java/lang/Object.html) other)

#### public static [Session](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session)
**extract** ([Intent](https://developer.android.com/reference/android/content/Intent.html) intent)

Extracts the session extra from the given intent, such as a callback intent received
after `https://developers.google.com/android/reference/com/google/android/gms/fitness/SessionsClient#registerForSessions(android.app.PendingIntent)` to session start/end notifications, or an intent to
`https://developers.google.com/android/reference/com/google/android/gms/fitness/SessionsApi.ViewIntentBuilder`.

##### Returns

- The extracted Session, or `null` if the given intent does not contain a Session.

#### public long **getActiveTime** ([TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit.html) timeUnit)

Returns the active time period of the session.

Make sure to use `https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#hasActiveTime()`
before using this method.

##### Throws

| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException.html) | `https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Session#hasActiveTime()` returns `false`. |
|---|---|

#### public [String](https://developer.android.com/reference/java/lang/String.html) **getActivity** ()

Returns the activity associated with this session, if set. Else returns
`https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities#UNKNOWN`.

#### public [String](https://developer.android.com/reference/java/lang/String.html) **getAppPackageName** ()

Returns the package name for the application responsible for adding the session. or
`null` if unset/unknown. The `https://developer.android.com/reference/android/content/pm/PackageManager.html`
can be used to query relevant data on the application, such as the name, icon, or
logo.

#### public [String](https://developer.android.com/reference/java/lang/String.html) **getDescription** ()

Returns the description for this session.

#### public long **getEndTime** ([TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit.html) 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](https://developer.android.com/reference/java/lang/String.html) **getIdentifier** ()

Returns the identifier for this session.

#### public static [String](https://developer.android.com/reference/java/lang/String.html) **getMimeType** ([String](https://developer.android.com/reference/java/lang/String.html) activity)

Returns the MIME type which describes a Session for a particular activity. The MIME
type is used in intents such as the `https://developers.google.com/android/reference/com/google/android/gms/fitness/SessionsApi.ViewIntentBuilder` intent.

##### Parameters

| activity | One of the activities in `https://developers.google.com/android/reference/com/google/android/gms/fitness/FitnessActivities`. |
|---|---|

#### public [String](https://developer.android.com/reference/java/lang/String.html) **getName** ()

Returns the name for this session, if set.

#### public long **getStartTime** ([TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit.html) 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](https://developer.android.com/reference/java/lang/String.html) **toString** ()

#### public void **writeToParcel** ([Parcel](https://developer.android.com/reference/android/os/Parcel.html) dest, int flags)