Class HealthSample
- Direct Known Subclasses:
BloodPressureSample, CategorySample, QuantitySample, SeriesSample, SessionSample
The base of every health record: what it measures and when.
Concrete subclasses correspond to HealthDataKind --
QuantitySample, CategorySample, SeriesSample and the
SessionSample family.
Instants and intervals
Every sample carries both a start and an end. When they are equal the
sample marks an instant -- see isInstantaneous(). Cumulative types
such as steps are always intervals; see
HealthDataType.isIntervalOnly().
Identity
getId() is assigned by the platform on write. It is scoped to this
platform and this installation and does not survive a reinstall, so
it must not be used as a primary key on your server.
getMetadata() is not persisted to HealthKit or Health Connect in
this release. It round-trips through the local store used by the
simulator, desktop and JavaScript, and it travels with a sample you
hold in memory, but a sample written to a mobile platform and read back
comes back without it. Health Connect has no arbitrary metadata --
only a single clientRecordId -- so a general map cannot be carried
there at all. Correlate on your own identifier held in your own
storage, keyed by whatever you can reconstruct from the sample's type,
time range and value.
Mutability
Samples you build for a write are mutable so optional fields can be
filled in before handing them to HealthStore. Samples returned from a
query are snapshots: changing one does not change the store, and to
modify stored data you delete and re-write it.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedHealthSample(HealthDataType type, long startMillis, long endMillis) Creates a sample spanning[startMillis, endMillis]. -
Method Summary
Modifier and TypeMethodDescriptionbooleanTwo samples are equal when they carry the same platform identifier.final longThe span in milliseconds; 0 for an instantaneous sample.final longEnd, epoch millis UTC.final StringgetId()The platform-assigned identifier, or null for a sample that has not been written yet.Free-form metadata carried alongside the sample, never null.final RecordingMethodHow this sample was recorded.final HealthSourceWhich app and device produced this sample, or null when the platform did not report it.final longInclusive start, epoch millis UTC.final HealthDataTypegetType()What this sample measures.inthashCode()Returns a hash code value for the object.final booleantruewhen this sample marks a moment rather than a span.final voidputMetadata(String key, String value) Attaches a metadata entry.final voidSets the platform identifier.final voidsetRecordingMethod(RecordingMethod recordingMethod) Declares how this sample was recorded.final voidsetSource(HealthSource source) Sets the originating source.toString()Returns a string representation of the object.
-
Constructor Details
-
HealthSample
Creates a sample spanning
[startMillis, endMillis].Throws
IllegalArgumentException: iftypeis null or the end precedes the start.
-
-
Method Details
-
getType
What this sample measures. -
getStartMillis
public final long getStartMillis()Inclusive start, epoch millis UTC. -
getEndMillis
public final long getEndMillis()End, epoch millis UTC. Equal to the start for an instantaneous sample. -
isInstantaneous
public final boolean isInstantaneous()truewhen this sample marks a moment rather than a span. -
getDurationMillis
public final long getDurationMillis()The span in milliseconds; 0 for an instantaneous sample. -
getId
The platform-assigned identifier, or null for a sample that has not been written yet. See the class documentation on identity before persisting this anywhere. -
setId
Sets the platform identifier. Called by ports when reading; setting it on a sample you are about to write has no effect. -
getSource
Which app and device produced this sample, or null when the platform did not report it. -
setSource
Sets the originating source. Called by ports when reading. -
getRecordingMethod
How this sample was recorded. Never null; defaults toRecordingMethod.UNKNOWN. -
setRecordingMethod
Declares how this sample was recorded. Worth setting on writes -- other apps use it to decide how much to trust a value. -
getMetadata
-
putMetadata
-
equals
-
hashCode
public int hashCode()Description copied from class:ObjectReturns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.) -
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())
-