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. True on watchOS, on iOS 26 and later, and on Wear OS.isSensorCollectionSupported()-- the OS also gathers heart rate and energy into that session by itself.
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; }
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.
falseon Android phones -- Health Connect has no such concept andandroidx.health.servicesis Wear OS only -- and on iOS before version 26.startSession(WorkoutConfiguration)still works there, 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. -
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.
-