Class AggregateQuery
Describes a bucketed summary read against HealthStore.
AggregateQuery q = new AggregateQuery()
.addType(HealthDataType.STEPS)
.addMetric(AggregateMetric.TOTAL)
.setTimeRange(HealthTimeRange.calendarDays(7, TimeZone.getDefault()))
.setBucket(HealthInterval.calendarDays(1, TimeZone.getDefault()));
Totals are not comparable across platforms
When a phone and a watch both record steps for the same walk, the store holds two overlapping sets of samples. HealthKit de-duplicates them in its statistics engine; Health Connect does not. The same query against the same person can therefore return roughly double on Android.
This API does not paper over that with a heuristic de-duplicator --
guessing which of two overlapping sources is authoritative is exactly
the kind of silent wrongness health data cannot afford. Use
addSource(String) to pin the query to the source you trust, and tell
the user which device a figure came from.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddMetric(AggregateMetric metric) Adds a metric to compute.Restricts the summary to one writing app -- see the double-counting warning on this class.addType(HealthDataType type) Adds a type to summarize.The bucket width, or null for one bucket over the whole range.The metrics this query computes.The source filter, empty when unrestricted.The span this query summarizes.getTypes()The types this query summarizes.getUnit()The requested unit, or null for canonical units.static booleanisMeaningful(HealthDataType type, AggregateMetric metric) Whethermetricsays anything true abouttype.setBucket(HealthInterval bucket) Splits the range into buckets of this width.setTimeRange(HealthTimeRange timeRange) The span to summarize.setUnit(HealthUnit unit) Returns aggregated values inunitrather than each type's canonical unit.voidvalidate()Validates the query and throws if it cannot be run.
-
Constructor Details
-
AggregateQuery
public AggregateQuery()
-
-
Method Details
-
addType
Adds a type to summarize. At least one is required. -
getTypes
The types this query summarizes. -
addMetric
Adds a metric to compute. At least one is required. -
getMetrics
The metrics this query computes. -
addSource
Restricts the summary to one writing app -- see the double-counting warning on this class. -
getSources
-
setTimeRange
The span to summarize. Required. -
getTimeRange
The span this query summarizes. -
setBucket
Splits the range into buckets of this width. Leave unset for a single bucket covering the whole range.
Prefer
HealthInterval.calendarDays(int,java.util.TimeZone)over a fixed 24-hour width whenever the buckets are labelled with dates in your UI -- seeHealthInterval. -
getBucket
The bucket width, or null for one bucket over the whole range. -
setUnit
Returns aggregated values inunitrather than each type's canonical unit. -
getUnit
The requested unit, or null for canonical units. -
validate
Validates the query and throws if it cannot be run.
Throws
HealthException:HealthError.INVALID_ARGUMENTfor a missing type, metric or range, or for a metric that is meaningless for the requested type;HealthError.UNIT_MISMATCHfor an incompatible unit.
- Throws:
HealthException
-
isMeaningful
Whether
metricsays anything true abouttype.AggregateMetric.COUNTandAggregateMetric.DURATIONapply to everything. Summing a discrete series -- the total of every body mass ever recorded -- and averaging a cumulative one -- the mean of arbitrarily-chunked step totals -- are both meaningless, so they are rejected rather than answered.
-