Class 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".
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic WorkoutSamplecreate(WorkoutActivityType activityType, long startMillis, long endMillis) The active portion of this workout, as aDuration.longTime actually exercising, excluding pauses.The activity, mapped onto this API's shared vocabulary.intThe raw platform activity constant -- anHKWorkoutActivityTypeon iOS or anExerciseSessionRecordexercise type on Android -- or-1when unknown.Distance covered, or null when not measured.Energy burned across the workout, or null when not measured.voidsetActiveDuration(Duration active) Sets the active portion of this workout.voidsetActiveDurationMillis(long activeDurationMillis) Sets the paused-time-excluded duration.voidsetPlatformCode(int platformCode) Records the raw platform activity constant.voidsetTotalDistance(HealthQuantity totalDistance) Sets the distance total.voidsetTotalEnergy(HealthQuantity totalEnergy) Sets the energy total.toString()Returns a string representation of the object.Methods inherited from class SessionSample
getNotes, getTitle, setNotes, setTitleMethods inherited from class HealthSample
equals, getDuration, getDurationMillis, getEnd, getEndMillis, getId, getMetadata, getRecordingMethod, getSource, getStart, getStartMillis, getType, hashCode, isInstantaneous, putMetadata, setId, setRecordingMethod, setSource
-
Field Details
-
WORKOUT_NOT_PERSISTED
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
HealthSample.getId()is populated.if (workout.getMetadata().containsKey( WorkoutSample.WORKOUT_NOT_PERSISTED)) { uploadToMyServer(workout); }- See Also:
-
SAMPLES_NOT_PERSISTED
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.
- See Also:
-
-
Method Details
-
create
public static WorkoutSample create(WorkoutActivityType activityType, long startMillis, long endMillis) -
getActivityType
The activity, mapped onto this API's shared vocabulary. SeegetPlatformCode()when you need exactly what the platform said. -
getPlatformCode
public int getPlatformCode()The raw platform activity constant -- an
HKWorkoutActivityTypeon iOS or anExerciseSessionRecordexercise type on Android -- or-1when unknown.This is the fidelity escape hatch for the deliberately partial
WorkoutActivityTypevocabulary. It is platform-specific by definition: the same integer means different things on the two platforms, so branch ongetActivityType()first and only reach for this when you must. -
setPlatformCode
public void setPlatformCode(int platformCode) Records the raw platform activity constant. Called by ports. -
getTotalEnergy
Energy burned across the workout, or null when not measured. -
setTotalEnergy
Sets the energy total.
Throws
IllegalArgumentException: if the quantity does not measure energy.
-
getTotalDistance
Distance covered, or null when not measured. -
setTotalDistance
Sets the distance total.
Throws
IllegalArgumentException: if the quantity does not measure length.
-
getActiveDurationMillis
public long getActiveDurationMillis()Time actually exercising, excluding pauses. Falls back to the wall duration when the platform did not report it separately. -
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
-
setActiveDuration
Sets the active portion of this workout. -
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())- Overrides:
toStringin classHealthSample
-