Added in API level 24

HealthStats


public class HealthStats
extends Object

java.lang.Object
   ↳ android.os.health.HealthStats


A HealthStats object contains system health data about an application.

Data Types
Each of the keys references data in one of five data types:

A measurement metric contains a single long value. That value may be a count, a time, or some other type of value. The unit for a measurement (COUNT, MS, etc) will always be in the name of the constant for the key to retrieve it. For example, the UidHealthStats.MEASUREMENT_WIFI_TX_MS value is the number of milliseconds (ms) that were spent transmitting on wifi by an application. The UidHealthStats.MEASUREMENT_MOBILE_RX_PACKETS measurement is the number of packets received on behalf of an application. The UidHealthStats.MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT measurement is the number of times the user touched the screen, causing the screen to stay awake.

A timer metric contains an int count and a long time, measured in milliseconds. Timers track how many times a resource was used, and the total duration for that usage. For example, the UidHealthStats.TIMER_FLASHLIGHT timer tracks how many times the application turned on the flashlight, and for how many milliseconds total it kept it on.

A measurement map metric is a mapping of String names to Long values. The names typically are application provided names. For example, the PackageHealthStats.MEASUREMENTS_WAKEUP_ALARMS_COUNT measurement map is a mapping of the tag provided to the AlarmManager when the alarm is scheduled.

A timer map metric is a mapping of String names to TimerStat objects. The names are typically application provided names. For example, the UidHealthStats.TIMERS_WAKELOCKS_PARTIAL is a mapping of tag provided to the PowerManager when the wakelock is created to the number of times and for how long each wakelock was active.

Lastly, a health stats metric is a mapping of String names to a recursive HealthStats object containing more detailed information. For example, the UidHealthStats.STATS_PACKAGES metric is a mapping of the package names for each of the APKs sharing a uid to the information recorded for that apk. The returned HealthStats objects will each be associated with a different set of constants. For the HealthStats returned for UidHealthStats.STATS_PACKAGES, the keys come from the PackageHealthStats class.

The keys that are available are subject to change, depending on what a particular device or software version is capable of recording. Applications must handle the absence of data without crashing.

Summary

Public methods

String getDataType()

Get a name representing the contents of this object.

long getMeasurement(int key)

Get the measurement for the given key.

int getMeasurementKeyAt(int index)

Get the key for the measurement at the given index.

int getMeasurementKeyCount()

Get the number of measurement values in this object.

Map<StringLong> getMeasurements(int key)

Get the measurements map for the given key.

int getMeasurementsKeyAt(int index)

Get the key for the measurement map at the given index.

int getMeasurementsKeyCount()

Get the number of measurement map values in this object.

Map<StringHealthStats> getStats(int key)

Get the HealthStats map for the given key.

int getStatsKeyAt(int index)

Get the key for the timer at the given index.

int getStatsKeyCount()

Get the number of HealthStat map values in this object.

TimerStat getTimer(int key)

Return a TimerStat object for the given key.

int getTimerCount(int key)

Get the count for the timer for the given key.

int getTimerKeyAt(int index)

Get the key for the timer at the given index.

int getTimerKeyCount()

Get the number of timer values in this object.

long getTimerTime(int key)

Get the time for the timer for the given key, in milliseconds.

Map<StringTimerStat> getTimers(int key)

Get the TimerStat map for the given key.

int getTimersKeyAt(int index)

Get the key for the timer map at the given index.

int getTimersKeyCount()

Get the number of timer map values in this object.

boolean hasMeasurement(int key)

Return whether this object contains a measurement for the supplied key.

boolean hasMeasurements(int key)

Return whether this object contains a measurements map for the supplied key.

boolean hasStats(int key)

Return whether this object contains a HealthStats map for the supplied key.

boolean hasTimer(int key)

Return whether this object contains a TimerStat for the supplied key.

boolean hasTimers(int key)

Return whether this object contains a timers map for the supplied key.

Inherited methods

Public methods

getDataType

Added in API level 24
public String getDataType ()

Get a name representing the contents of this object.

Returns
String

getMeasurement

Added in API level 24
public long getMeasurement (int key)

Get the measurement for the given key.

Parameters
key int

Returns
long

Throws
IndexOutOfBoundsException When the key is not present in this object.

getMeasurementKeyAt

Added in API level 24
public int getMeasurementKeyAt (int index)

Get the key for the measurement at the given index. Index must be between 0 and the result of getMeasurementKeyCount().

Parameters
index int

Returns
int

getMeasurementKeyCount

Added in API level 24
public int getMeasurementKeyCount ()

Get the number of measurement values in this object. Can be used to iterate through the available measurements.

Returns
int

getMeasurements

Added in API level 24
public Map<StringLong> getMeasurements (int key)

Get the measurements map for the given key.

Parameters
key int

Returns
Map<StringLong>

Throws
IndexOutOfBoundsException When the key is not present in this object.

getMeasurementsKeyAt

Added in API level 24
public int getMeasurementsKeyAt (int index)

Get the key for the measurement map at the given index. Index must be between 0 and the result of getMeasurementsKeyCount().

Parameters
index int

Returns
int

getMeasurementsKeyCount

Added in API level 24
public int getMeasurementsKeyCount ()

Get the number of measurement map values in this object. Can be used to iterate through the available measurement maps.

Returns
int

getStats

Added in API level 24
public Map<StringHealthStats> getStats (int key)

Get the HealthStats map for the given key.

Parameters
key int

Returns
Map<StringHealthStats>

Throws
IndexOutOfBoundsException When the key is not present in this object.

getStatsKeyAt

Added in API level 24
public int getStatsKeyAt (int index)

Get the key for the timer at the given index. Index must be between 0 and the result of getStatsKeyCount().

Parameters
index int

Returns
int

See also:

getStatsKeyCount

Added in API level 24
public int getStatsKeyCount ()

Get the number of HealthStat map values in this object. Can be used to iterate through the available measurements.

Returns
int

getTimer

Added in API level 24
public TimerStat getTimer (int key)

Return a TimerStat object for the given key. This will allocate a new TimerStat object, which may be wasteful. Instead, use getTimerCount(int) and getTimerTime(int).

Parameters
key int

Returns
TimerStat

Throws
IndexOutOfBoundsException When the key is not present in this object.

getTimerCount

Added in API level 24
public int getTimerCount (int key)

Get the count for the timer for the given key.

Parameters
key int

Returns
int

Throws
IndexOutOfBoundsException When the key is not present in this object.

getTimerKeyAt

Added in API level 24
public int getTimerKeyAt (int index)

Get the key for the timer at the given index. Index must be between 0 and the result of getTimerKeyCount().

Parameters
index int

Returns
int

See also:

getTimerKeyCount

Added in API level 24
public int getTimerKeyCount ()

Get the number of timer values in this object. Can be used to iterate through the available timers.

Returns
int

See also:

getTimerTime

Added in API level 24
public long getTimerTime (int key)

Get the time for the timer for the given key, in milliseconds.

Parameters
key int

Returns
long

Throws
IndexOutOfBoundsException When the key is not present in this object.

getTimers

Added in API level 24
public Map<StringTimerStat> getTimers (int key)

Get the TimerStat map for the given key.

Parameters
key int

Returns
Map<StringTimerStat>

Throws
IndexOutOfBoundsException When the key is not present in this object.

getTimersKeyAt

Added in API level 24
public int getTimersKeyAt (int index)

Get the key for the timer map at the given index. Index must be between 0 and the result of getTimersKeyCount().

Parameters
index int

Returns
int

getTimersKeyCount

Added in API level 24
public int getTimersKeyCount ()

Get the number of timer map values in this object. Can be used to iterate through the available timer maps.

Returns
int

hasMeasurement

Added in API level 24
public boolean hasMeasurement (int key)

Return whether this object contains a measurement for the supplied key.

Parameters
key int

Returns
boolean

hasMeasurements

Added in API level 24
public boolean hasMeasurements (int key)

Return whether this object contains a measurements map for the supplied key.

Parameters
key int

Returns
boolean

hasStats

Added in API level 24
public boolean hasStats (int key)

Return whether this object contains a HealthStats map for the supplied key.

Parameters
key int

Returns
boolean

hasTimer

Added in API level 24
public boolean hasTimer (int key)

Return whether this object contains a TimerStat for the supplied key.

Parameters
key int

Returns
boolean

hasTimers

Added in API level 24
public boolean hasTimers (int key)

Return whether this object contains a timers map for the supplied key.

Parameters
key int

Returns
boolean