public final class WorkoutSample
- Object
- HealthSample
- SessionSample
- WorkoutSample
A completed workout: what kind, how long, and the totals the platform computed for it.
Totals are nullable on purpose. A workout recorded without a heart-rate
sensor has no energy total, and reporting that as zero would put a
false 0 kcal into every summary. null means “not measured”; zero
means “measured, and it was zero”.
Fields
public static final String WORKOUT_NOT_PERSISTED = "cn1.workoutNotPersisted" | Creates a workout spanning [startMillis, endMillis]. |
public static final String SAMPLES_NOT_PERSISTED = "cn1.workout.samplesNotPersisted" | Metadata key naming the data types the platform refused to store, comma separated, or absent when everything fed in was persisted. |
Methods
Inherited methods
Field details
WORKOUT_NOT_PERSISTED
public static final String WORKOUT_NOT_PERSISTED = "cn1.workoutNotPersisted"Creates a workout spanning [startMillis, endMillis].
Metadata key set on a workout the platform could not store as a
session record of its own.
Neither HealthKit nor the Health Connect bridge accepts a workout
through the sample write path in this release. The child
measurements are persisted; the workout comes back for you to keep
or upload. Check for this rather than assuming getId() is
populated.
if (workout.getMetadata().containsKey(
WorkoutSample.WORKOUT_NOT_PERSISTED)) {
uploadToMyServer(workout);
}
SAMPLES_NOT_PERSISTED
public static final String SAMPLES_NOT_PERSISTED = "cn1.workout.samplesNotPersisted"Metadata key naming the data types the platform refused to store, comma separated, or absent when everything fed in was persisted.
Health Connect has no single-value write form for the series-shaped types – power, speed and both cadences – which is exactly what a bike or foot pod feeds into a workout. Those samples cannot be stored there, so the workout names them rather than dropping them silently and resolving as though nothing had happened.
Method details
create
public static WorkoutSample create(WorkoutActivityType activityType, long startMillis, long endMillis)getActivityType
public WorkoutActivityType getActivityType()getPlatformCode() when you need exactly what the platform said.getPlatformCode
public int getPlatformCode()The raw platform activity constant – an HKWorkoutActivityType on
iOS or an ExerciseSessionRecord exercise type on Android – or
-1 when unknown.
This is the fidelity escape hatch for the deliberately partial
WorkoutActivityType vocabulary. It is platform-specific by
definition: the same integer means different things on the two
platforms, so branch on getActivityType() first and only reach
for this when you must.
setPlatformCode
public void setPlatformCode(int platformCode)getTotalEnergy
public HealthQuantity getTotalEnergy()setTotalEnergy
public void setTotalEnergy(HealthQuantity totalEnergy)Throws
IllegalArgumentException- if the quantity does not measure energy.
getTotalDistance
public HealthQuantity getTotalDistance()setTotalDistance
public void setTotalDistance(HealthQuantity totalDistance)Throws
IllegalArgumentException- if the quantity does not measure length.
getActiveDurationMillis
public long getActiveDurationMillis()setActiveDurationMillis
public void setActiveDurationMillis(long activeDurationMillis)Sets the paused-time-excluded duration.
Any negative value means “not reported separately”, which is what
getActiveDurationMillis() answers with the wall duration.
Throws
IllegalArgumentException- if the value exceeds the workout’s own duration.
getActiveDuration
public Duration getActiveDuration()The active portion of this workout, as a Duration.
The millis pair stays for the ports and the wire format; this is the type the rest of the framework speaks.
setActiveDuration
public void setActiveDuration(Duration active)toString
public String toString()