Class WorkoutManager

java.lang.Object
com.codename1.health.workout.WorkoutManager

public class WorkoutManager extends Object

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:

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 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 on Android phones -- Health Connect has no such concept and androidx.health.services is 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

      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()
      The session currently running or paused, or null.
    • createSession

      protected WorkoutSession createSession(WorkoutConfiguration config)
      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.