Class SampleQuery
Describes a read against HealthStore. Fluent setters return this.
SampleQuery q = new SampleQuery()
.addType(HealthDataType.HEART_RATE)
.setTimeRange(HealthTimeRange.lastHours(24))
.setSortDescending(true)
.setLimit(500);
Always set a limit for high-frequency types
A year of continuous heart rate is on the order of half a million
samples. The default limit of 10,000 exists so that a naive query
cannot exhaust the heap on a phone; raise it deliberately, and prefer
paging through HealthStore.readSamplePage(SampleQuery) over asking
for everything at once.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe limit applied when none is set. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRestricts the query to samples written by one app, identified by its bundle id or package name -- seeHealthSource.getBundleId().addType(HealthDataType type) Adds a type to read.intgetLimit()The sample cap.The continuation token, or null to start from the beginning.The gap that separates two sleep sessions, as a
.invalid reference
DurationlongThe sleep-session grouping gap in milliseconds.The source filter, empty when unrestricted.The span this query reads, or null when unset.getTypes()The types this query reads.getUnit()The requested unit, or null for each type's canonical unit.booleantruewhen series are expanded into individual samples.booleantruewhen results come back newest first.setFlattenSeries(boolean flattenSeries) Whether to expandSeriesSamplerecords into individualQuantitySampleobjects.setLimit(int limit) Caps how many samples come back.setPageToken(String pageToken) Continues a previous read fromSamplePage.getNextPageToken().setSleepSessionGap(Duration value) The gap that separates two sleep sessions.setSleepSessionGapMillis(long gapMillis) setSortDescending(boolean sortDescending) Returns the newest samples first.setTimeRange(HealthTimeRange timeRange) The span to read.setUnit(HealthUnit unit) Returns values inunitinstead of the type's canonical unit.voidvalidate()Validates the query and throws if it cannot be run.
-
Field Details
-
DEFAULT_LIMIT
public static final int DEFAULT_LIMITThe limit applied when none is set.- See Also:
-
-
Constructor Details
-
SampleQuery
public SampleQuery()
-
-
Method Details
-
addType
Adds a type to read. At least one is required. -
getTypes
The types this query reads. -
addSource
Restricts the query to samples written by one app, identified by its bundle id or package name -- see
HealthSource.getBundleId(). Call more than once to allow several.Worth doing when a phone and a watch both record the same activity: see the double-counting warning on
AggregateQuery. -
getSources
-
setTimeRange
The span to read. Required. -
getTimeRange
The span this query reads, or null when unset. -
setLimit
Caps how many samples come back. Must be positive.
On Android the cap is per data type when a query names several. Health Connect pages per record type, so a limit of ten over two types can return twenty. Neither alternative works: dividing the budget cannot find a newest-ten that all lives in one type, and trimming the merged page discards records the per-type continuation tokens have already moved past. Honouring it exactly needs an incremental k-way merge across the types, which is not implemented yet. Query one type at a time where the cap has to be exact -- which is also what iOS does, since
HKSampleQueryreads one type per query. -
getLimit
public int getLimit()The sample cap. -
setSortDescending
Returns the newest samples first. Default is oldest first. -
isSortDescending
public boolean isSortDescending()truewhen results come back newest first. -
setUnit
Returns values in
unitinstead of the type's canonical unit.Throws
The unit is validated when the query runs, not here: a unit that measures the wrong dimension fails with
HealthError.UNIT_MISMATCHbefore the platform is touched. -
getUnit
The requested unit, or null for each type's canonical unit. -
setFlattenSeries
Whether to expand
SeriesSamplerecords into individualQuantitySampleobjects. Defaults totrue.Leave it on and both platforms return the same thing, so your code is identical across them. Turn it off when you need a series' record identity -- to delete it, for instance.
Only Health Connect and the local stores group measurements into records, so only they have anything to withhold. HealthKit stores each measurement separately and the iOS port returns ordinary
QuantitySampleobjects whichever way this is set -- not one-point series -- so code that turns flattening off and then casts toSeriesSamplefails there. Test the type rather than assuming it. -
isFlattenSeries
public boolean isFlattenSeries()truewhen series are expanded into individual samples. -
setSleepSessionGap
The gap that separates two sleep sessions.
The
form ofinvalid reference
DurationsetSleepSessionGapMillis(long), which is the type the rest ofthe framework speaks; the millis form stays for the ports and the wire format.
-
getSleepSessionGap
The gap that separates two sleep sessions, as a
.invalid reference
Duration -
setSleepSessionGapMillis
-
getSleepSessionGapMillis
public long getSleepSessionGapMillis()The sleep-session grouping gap in milliseconds. -
setPageToken
Continues a previous read fromSamplePage.getNextPageToken(). -
getPageToken
The continuation token, or null to start from the beginning. -
validate
Validates the query and throws if it cannot be run.
Called by
HealthStorebefore the platform is touched, so a malformed query fails immediately and locally rather than as an opaque platform error later.Throws
HealthException: withHealthError.INVALID_ARGUMENTfor a missing type or range or a non-positive limit, andHealthError.UNIT_MISMATCHwhen the requested unit does not match a requested type's dimension.
- Throws:
HealthException
-