Class WorkoutManager
Starts and tracks workout recordings.
Obtain one from Health.getWorkouts(); it is
never null.
Check what the platform will actually do for you
Two capabilities that are easy to conflate, and that this API keeps separate on purpose:
isLiveSessionSupported()-- the OS runs a real session, keeping the app alive while it records.isSensorCollectionSupported()-- the OS also gathers heart rate and energy into that session by itself.
Both are false on every platform in this release. No port
implements an OS-owned session yet, so every workout is recorded: the
framework keeps the clock and the rollup, and persists what you feed
it when the session ends. That is exactly the flow Google documents
for Android phones, and it is why these are queryable facts rather
than assumptions -- code that branches on them today keeps working
unchanged when a port starts answering true.
Where the second is false, a workout records only what you feed it. Building a UI with a live heart-rate readout without checking would produce an app that works on a watch and shows a permanent dash on a phone.
WorkoutManager workouts = Health.getInstance().getWorkouts();
workouts.startSession(new WorkoutConfiguration()
.setActivityType(WorkoutActivityType.RUNNING)
.setLocationType(WorkoutLocationType.OUTDOOR))
.onResult((session, err) -> {
if (err != null) { Log.e(err); return; }
// startSession() only builds the session; this starts the
// clock and moves it to RUNNING, and until it is called
// addSamples() and end() fail with SESSION_STATE.
session.start();
if (!session.isLive()) {
status.setText("Recording - connect a strap for heart rate");
}
});
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected WorkoutSessioncreateSession(WorkoutConfiguration config) Creates the session object.final WorkoutSessionThe session currently running or paused, or null.booleanWhether the operating system provides a real workout session that keeps the app alive and owns the recording.booleanWhether the operating system collects sensor data into the session on its own, without the app feeding samples in.final AsyncResource<WorkoutSession> startSession(WorkoutConfiguration configuration) Starts a workout.
-
Constructor Details
-
WorkoutManager
public WorkoutManager()
-
-
Method Details
-
isLiveSessionSupported
public boolean isLiveSessionSupported()Whether the operating system provides a real workout session that keeps the app alive and owns the recording.
falseeverywhere in this release: no port overrides this, so a workout is always recorded by the framework rather than owned by the OS. Health Connect has no such concept at all on phones, andandroidx.health.servicesis Wear OS only; HealthKit does haveHKWorkoutSession, but nothing here drives it yet.startSession(WorkoutConfiguration)works regardless, in recorded mode. -
isSensorCollectionSupported
public boolean isSensorCollectionSupported()Whether the operating system collects sensor data into the session on its own, without the app feeding samples in.
falseon every platform in this release. It is the OS-owned session that would do the collecting, and no port runs one yet -- so a workout contains what you fed it throughWorkoutSession.addSamples(java.util.List), and nothing else. -
startSession
Starts a workout.
Only one session may run at a time; starting another while one is active fails with
HealthError.SESSION_STATErather than silently abandoning the first, because an abandoned workout is data the user believed was being recorded. -
getActiveSession
The session currently running or paused, or null. -
createSession
Creates the session object. Ports override to return a session backed by a real platform workout; the default records in shared code and writes on end.
-