Class WorkoutSession
A workout being recorded.
Obtained from
WorkoutManager.startSession(WorkoutConfiguration). The state machine,
the elapsed clock and the rollup of fed samples live here in shared
code; ports implement only the do* methods.
Live and recorded sessions
isLive() tells you whether the operating system is running a real
workout session -- keeping the app alive and collecting sensor data
itself. That is the case on watchOS, on iOS 26 and later, and on Wear
OS.
On an Android phone it is not, because Health Connect has no such
concept: Google's documented approach there is to record the session
yourself and write it when it ends, which is exactly what a recorded
session does. So a recorded session is not a degraded shim -- it is the
platform-correct design -- but it does mean nothing is collected
unless you feed it, through addSamples(List) or by attaching a
Bluetooth sensor with
SensorSessionOptions.setWorkoutSession(WorkoutSession).
Check WorkoutManager.isSensorCollectionSupported() before building UI
that assumes a heart rate will appear on its own.
A killed workout is over
Sessions are deliberately not restored after the process dies. end()
was never called, so nothing is written. An app that needs
crash-resilient workouts should write partial records as it goes --
which is what the recorded path does anyway.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedWorkoutSession(WorkoutConfiguration configuration) Ports and the framework construct sessions. -
Method Summary
Modifier and TypeMethodDescriptionfinal AsyncResource<Boolean> addEvent(WorkoutEvent event) Records an event -- a lap, a marker, a segment boundary.final voidaddListener(WorkoutSessionListener listener) Registers a listener for state, statistics and events.final AsyncResource<Boolean> addSamples(List<HealthSample> samples) Feeds samples into the workout.final voiddiscard()Abandons the workout without writing anything.protected voiddoAddSamples(List<HealthSample> samples, AsyncResource<Boolean> out) Hands fed samples to the platform.protected abstract voidAbandons the workout without persisting.protected abstract voiddoEnd(AsyncResource<WorkoutSample> out) Stops collection and persists the workout.protected abstract voiddoPause(AsyncResource<Boolean> out) Pauses platform collection.protected voiddoPrepare(AsyncResource<Boolean> out) Warms up sensors.protected abstract voiddoResume(AsyncResource<Boolean> out) Resumes platform collection.protected abstract voiddoStart(AsyncResource<Boolean> out) Starts platform collection.final AsyncResource<WorkoutSample> end()Ends the workout and writes it to the health store, resolving with the persisted record.protected final voidfireEvent(WorkoutEvent event) Notifies listeners of an event.protected final voidfireFailed(HealthException error) Reports an unrecoverable failure and moves toWorkoutSessionState.FAILED.protected final voidNotifies listeners that a statistic changed.final WorkoutConfigurationThe configuration this session was started with.final longTime spent recording, excluding pauses.final longWhen it ended, epoch millis, or 0 while still running.final List<WorkoutEvent> Every event recorded so far.final longWhen the workout started, epoch millis, or 0 beforestart().final WorkoutSessionStategetState()The current state.final HealthQuantitygetStatistic(HealthDataType type, AggregateMetric metric) A live statistic, ornullwhen the platform does not compute it and nothing has been fed in.protected final Map<String, HealthQuantity> The accumulated statistics, for ports assembling the final record.booleanisLive()Whether the operating system is running a real workout session -- see the class documentation.final AsyncResource<Boolean> pause()Pauses recording.final AsyncResource<Boolean> prepare()Warms up sensors ahead ofstart(), for apps that show a countdown.final voidremoveListener(WorkoutSessionListener listener) Removes a listener.final AsyncResource<Boolean> resume()Resumes after a pause.protected final voidsetState(WorkoutSessionState newState) Moves to a new state and notifies listeners.final AsyncResource<Boolean> start()Starts recording.
-
Constructor Details
-
WorkoutSession
Ports and the framework construct sessions.
-
-
Method Details
-
getConfiguration
The configuration this session was started with. -
getState
The current state. -
isLive
public boolean isLive()Whether the operating system is running a real workout session -- see the class documentation.falsemeans the clock and the saved record are real but collection is entirely up to you. -
prepare
Warms up sensors ahead ofstart(), for apps that show a countdown. Optional;start()works without it. -
start
Starts recording. -
pause
Pauses recording. The elapsed clock stops. -
resume
Resumes after a pause. -
end
Ends the workout and writes it to the health store, resolving with the persisted record. -
discard
public final void discard()Abandons the workout without writing anything. -
getElapsedMillis
public final long getElapsedMillis()Time spent recording, excluding pauses. -
getStartedAtMillis
public final long getStartedAtMillis()When the workout started, epoch millis, or 0 beforestart(). -
getEndedAtMillis
public final long getEndedAtMillis()When it ended, epoch millis, or 0 while still running. -
getStatistic
A live statistic, or
nullwhen the platform does not compute it and nothing has been fed in.Never fabricates a zero. On a recorded session with no sensor attached every statistic is null, and showing "0 bpm" instead would be a claim the app cannot support.
-
addSamples
Feeds samples into the workout. On a recorded session this is the only way anything is collected. -
addEvent
Records an event -- a lap, a marker, a segment boundary. -
getEvents
Every event recorded so far. -
addListener
Registers a listener for state, statistics and events. -
removeListener
Removes a listener. -
getStatisticsSnapshot
The accumulated statistics, for ports assembling the final record. -
doPrepare
Warms up sensors. Default completes immediately. -
doStart
Starts platform collection. -
doPause
Pauses platform collection. -
doResume
Resumes platform collection. -
doEnd
Stops collection and persists the workout. -
doDiscard
protected abstract void doDiscard()Abandons the workout without persisting. -
doAddSamples
Hands fed samples to the platform. Default accepts them into the shared rollup only. -
setState
Moves to a new state and notifies listeners. Ports call this for transitions the platform initiated. -
fireStatisticsUpdated
Notifies listeners that a statistic changed. -
fireEvent
Notifies listeners of an event. -
fireFailed
Reports an unrecoverable failure and moves toWorkoutSessionState.FAILED.
-