public class WorkoutManager
- Object
- 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");
}
});
Constructors
public WorkoutManager() |
Methods
public boolean isLiveSessionSupported() | Whether the operating system provides a real workout session that keeps the app alive and owns the recording. |
public boolean isSensorCollectionSupported() | Whether the operating system collects sensor data into the session on its own, without the app feeding samples in. |
public final AsyncResource<WorkoutSession> startSession(WorkoutConfiguration configuration) | Starts a workout. |
public final WorkoutSession getActiveSession() | The session currently running or paused, or null. |
protected WorkoutSession createSession(WorkoutConfiguration config) | Creates the session object. |
Inherited methods
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.
false everywhere 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, and
androidx.health.services is Wear OS only; HealthKit does have
HKWorkoutSession, 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.
false on 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 through
WorkoutSession.addSamples(java.util.List), and nothing else.
startSession
public final AsyncResource<WorkoutSession> startSession(WorkoutConfiguration configuration)Starts a workout.
Only one session may run at a time; starting another while one is
active fails with HealthError.SESSION_STATE rather than silently
abandoning the first, because an abandoned workout is data the user
believed was being recorded.
getActiveSession
public final WorkoutSession getActiveSession()createSession
protected WorkoutSession createSession(WorkoutConfiguration config)