BatteryManager
public
class
BatteryManager
extends Object
java.lang.Object | |
↳ | android.os.BatteryManager |
The BatteryManager class contains strings and constants used for values
in the Intent.ACTION_BATTERY_CHANGED
Intent, and
provides a method for querying battery and charging properties.
Summary
Constants | |
---|---|
String |
ACTION_CHARGING
Sent when the device's battery has started charging (or has reached full charge and the device is on power). |
String |
ACTION_DISCHARGING
Sent when the device's battery may be discharging, so apps should avoid doing extraneous work that would cause it to discharge faster. |
int |
BATTERY_CAPACITY_LEVEL_CRITICAL
Battery capacity level is critical. |
int |
BATTERY_CAPACITY_LEVEL_FULL
Battery capacity level is full. |
int |
BATTERY_CAPACITY_LEVEL_HIGH
Battery capacity level is high. |
int |
BATTERY_CAPACITY_LEVEL_LOW
Battery capacity level is low. |
int |
BATTERY_CAPACITY_LEVEL_NORMAL
Battery capacity level is normal. |
int |
BATTERY_CAPACITY_LEVEL_UNKNOWN
Battery capacity level is unknown. |
int |
BATTERY_CAPACITY_LEVEL_UNSUPPORTED
Battery capacity level is unsupported. |
int |
BATTERY_HEALTH_COLD
|
int |
BATTERY_HEALTH_DEAD
|
int |
BATTERY_HEALTH_GOOD
|
int |
BATTERY_HEALTH_OVERHEAT
|
int |
BATTERY_HEALTH_OVER_VOLTAGE
|
int |
BATTERY_HEALTH_UNKNOWN
|
int |
BATTERY_HEALTH_UNSPECIFIED_FAILURE
|
int |
BATTERY_PLUGGED_AC
Power source is an AC charger. |
int |
BATTERY_PLUGGED_DOCK
Power source is dock. |
int |
BATTERY_PLUGGED_USB
Power source is a USB port. |
int |
BATTERY_PLUGGED_WIRELESS
Power source is wireless. |
int |
BATTERY_PROPERTY_CAPACITY
Remaining battery capacity as an integer percentage of total capacity (with no fractional part). |
int |
BATTERY_PROPERTY_CHARGE_COUNTER
Battery capacity in microampere-hours, as an integer. |
int |
BATTERY_PROPERTY_CURRENT_AVERAGE
Average battery current in microamperes, as an integer. |
int |
BATTERY_PROPERTY_CURRENT_NOW
Instantaneous battery current in microamperes, as an integer. |
int |
BATTERY_PROPERTY_ENERGY_COUNTER
Battery remaining energy in nanowatt-hours, as a long integer. |
int |
BATTERY_PROPERTY_STATUS
Battery charge status, from a BATTERY_STATUS_* value. |
int |
BATTERY_STATUS_CHARGING
|
int |
BATTERY_STATUS_DISCHARGING
|
int |
BATTERY_STATUS_FULL
|
int |
BATTERY_STATUS_NOT_CHARGING
|
int |
BATTERY_STATUS_UNKNOWN
|
String |
EXTRA_BATTERY_LOW
Extra for |
String |
EXTRA_CAPACITY_LEVEL
Extra for |
String |
EXTRA_CHARGING_STATUS
Extra for |
String |
EXTRA_CYCLE_COUNT
Extra for |
String |
EXTRA_HEALTH
Extra for |
String |
EXTRA_ICON_SMALL
Extra for |
String |
EXTRA_LEVEL
Extra for |
String |
EXTRA_PLUGGED
Extra for |
String |
EXTRA_PRESENT
Extra for |
String |
EXTRA_SCALE
Extra for |
String |
EXTRA_STATUS
Extra for |
String |
EXTRA_TECHNOLOGY
Extra for |
String |
EXTRA_TEMPERATURE
Extra for |
String |
EXTRA_VOLTAGE
Extra for |
Public methods | |
---|---|
long
|
computeChargeTimeRemaining()
Compute an approximation for how much time (in milliseconds) remains until the battery is fully charged. |
int
|
getIntProperty(int id)
Return the value of a battery property of integer type. |
long
|
getLongProperty(int id)
Return the value of a battery property of long type If the platform does not provide the property queried, this value will be Long.MIN_VALUE. |
String
|
getStringProperty(int id)
Return the value of a battery property of String type. |
boolean
|
isCharging()
Return true if the battery is currently considered to be charging. |
Inherited methods | |
---|---|
Constants
ACTION_CHARGING
public static final String ACTION_CHARGING
Sent when the device's battery has started charging (or has reached full charge
and the device is on power). This is a good time to do work that you would like to
avoid doing while on battery (that is to avoid draining the user's battery due to
things they don't care enough about).
This is paired with ACTION_DISCHARGING
. The current state can always
be retrieved with isCharging()
.
Constant Value: "android.os.action.CHARGING"
ACTION_DISCHARGING
public static final String ACTION_DISCHARGING
Sent when the device's battery may be discharging, so apps should avoid doing
extraneous work that would cause it to discharge faster.
This is paired with ACTION_CHARGING
. The current state can always
be retrieved with isCharging()
.
Constant Value: "android.os.action.DISCHARGING"
BATTERY_CAPACITY_LEVEL_CRITICAL
public static final int BATTERY_CAPACITY_LEVEL_CRITICAL
Battery capacity level is critical. @see EXTRA_CAPACITY_LEVEL
Constant Value: 1 (0x00000001)
BATTERY_CAPACITY_LEVEL_FULL
public static final int BATTERY_CAPACITY_LEVEL_FULL
Battery capacity level is full. @see EXTRA_CAPACITY_LEVEL
Constant Value: 5 (0x00000005)
BATTERY_CAPACITY_LEVEL_HIGH
public static final int BATTERY_CAPACITY_LEVEL_HIGH
Battery capacity level is high. @see EXTRA_CAPACITY_LEVEL
Constant Value: 4 (0x00000004)
BATTERY_CAPACITY_LEVEL_LOW
public static final int BATTERY_CAPACITY_LEVEL_LOW
Battery capacity level is low. @see EXTRA_CAPACITY_LEVEL
Constant Value: 2 (0x00000002)
BATTERY_CAPACITY_LEVEL_NORMAL
public static final int BATTERY_CAPACITY_LEVEL_NORMAL
Battery capacity level is normal. @see EXTRA_CAPACITY_LEVEL
Constant Value: 3 (0x00000003)
BATTERY_CAPACITY_LEVEL_UNKNOWN
public static final int BATTERY_CAPACITY_LEVEL_UNKNOWN
Battery capacity level is unknown. @see EXTRA_CAPACITY_LEVEL
Constant Value: 0 (0x00000000)
BATTERY_CAPACITY_LEVEL_UNSUPPORTED
public static final int BATTERY_CAPACITY_LEVEL_UNSUPPORTED
Battery capacity level is unsupported. @see EXTRA_CAPACITY_LEVEL
Constant Value: -1 (0xffffffff)
BATTERY_HEALTH_COLD
public static final int BATTERY_HEALTH_COLD
Constant Value: 7 (0x00000007)
BATTERY_HEALTH_DEAD
public static final int BATTERY_HEALTH_DEAD
Constant Value: 4 (0x00000004)
BATTERY_HEALTH_GOOD
public static final int BATTERY_HEALTH_GOOD
Constant Value: 2 (0x00000002)
BATTERY_HEALTH_OVERHEAT
public static final int BATTERY_HEALTH_OVERHEAT
Constant Value: 3 (0x00000003)
BATTERY_HEALTH_OVER_VOLTAGE
public static final int BATTERY_HEALTH_OVER_VOLTAGE
Constant Value: 5 (0x00000005)
BATTERY_HEALTH_UNKNOWN
public static final int BATTERY_HEALTH_UNKNOWN
Constant Value: 1 (0x00000001)
BATTERY_HEALTH_UNSPECIFIED_FAILURE
public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE
Constant Value: 6 (0x00000006)
BATTERY_PLUGGED_AC
public static final int BATTERY_PLUGGED_AC
Power source is an AC charger.
Constant Value: 1 (0x00000001)
BATTERY_PLUGGED_DOCK
public static final int BATTERY_PLUGGED_DOCK
Power source is dock.
Constant Value: 8 (0x00000008)
BATTERY_PLUGGED_USB
public static final int BATTERY_PLUGGED_USB
Power source is a USB port.
Constant Value: 2 (0x00000002)
BATTERY_PLUGGED_WIRELESS
public static final int BATTERY_PLUGGED_WIRELESS
Power source is wireless.
Constant Value: 4 (0x00000004)
BATTERY_PROPERTY_CAPACITY
public static final int BATTERY_PROPERTY_CAPACITY
Remaining battery capacity as an integer percentage of total capacity (with no fractional part).
Constant Value: 4 (0x00000004)
BATTERY_PROPERTY_CHARGE_COUNTER
public static final int BATTERY_PROPERTY_CHARGE_COUNTER
Battery capacity in microampere-hours, as an integer.
Constant Value: 1 (0x00000001)
BATTERY_PROPERTY_CURRENT_AVERAGE
public static final int BATTERY_PROPERTY_CURRENT_AVERAGE
Average battery current in microamperes, as an integer. Positive values indicate net current entering the battery from a charge source, negative values indicate net current discharging from the battery. The time period over which the average is computed may depend on the fuel gauge hardware and its configuration.
Constant Value: 3 (0x00000003)
BATTERY_PROPERTY_CURRENT_NOW
public static final int BATTERY_PROPERTY_CURRENT_NOW
Instantaneous battery current in microamperes, as an integer. Positive values indicate net current entering the battery from a charge source, negative values indicate net current discharging from the battery.
Constant Value: 2 (0x00000002)
BATTERY_PROPERTY_ENERGY_COUNTER
public static final int BATTERY_PROPERTY_ENERGY_COUNTER
Battery remaining energy in nanowatt-hours, as a long integer.
Constant Value: 5 (0x00000005)
BATTERY_PROPERTY_STATUS
public static final int BATTERY_PROPERTY_STATUS
Battery charge status, from a BATTERY_STATUS_* value.
Constant Value: 6 (0x00000006)
BATTERY_STATUS_CHARGING
public static final int BATTERY_STATUS_CHARGING
Constant Value: 2 (0x00000002)
BATTERY_STATUS_DISCHARGING
public static final int BATTERY_STATUS_DISCHARGING
Constant Value: 3 (0x00000003)
BATTERY_STATUS_FULL
public static final int BATTERY_STATUS_FULL
Constant Value: 5 (0x00000005)
BATTERY_STATUS_NOT_CHARGING
public static final int BATTERY_STATUS_NOT_CHARGING
Constant Value: 4 (0x00000004)
BATTERY_STATUS_UNKNOWN
public static final int BATTERY_STATUS_UNKNOWN
Constant Value: 1 (0x00000001)
EXTRA_BATTERY_LOW
public static final String EXTRA_BATTERY_LOW
Extra for Intent.ACTION_BATTERY_CHANGED
:
Boolean field indicating whether the battery is currently considered to be
low, that is whether a Intent.ACTION_BATTERY_LOW
broadcast
has been sent.
Constant Value: "battery_low"
EXTRA_CAPACITY_LEVEL
public static final String EXTRA_CAPACITY_LEVEL
Extra for Intent.ACTION_BATTERY_CHANGED
:
Int value representing the battery's capacity level. These constants are key indicators of
battery status and system capabilities, guiding power management decisions for both the
system and apps:
BATTERY_CAPACITY_LEVEL_UNSUPPORTED
: Feature not supported on this device.
BATTERY_CAPACITY_LEVEL_UNKNOWN
: Battery status is unavailable or uninitialized.
BATTERY_CAPACITY_LEVEL_CRITICAL
: Battery is critically low and the Android
framework has been notified to schedule a shutdown by this value
BATTERY_CAPACITY_LEVEL_LOW
: Android framework must limit background jobs to
avoid impacting charging speed
BATTERY_CAPACITY_LEVEL_NORMAL
: Battery level and charging rates are normal,
battery temperature is within normal range and adapter power is enough to charge the
battery at an acceptable rate. Android framework can run light background tasks without
affecting charging performance severely.
BATTERY_CAPACITY_LEVEL_HIGH
: Battery level is high, battery temperature is
within normal range and adapter power is enough to charge the battery at an acceptable
rate while running background loads. Android framework can run background tasks without
affecting charging or battery performance.
BATTERY_CAPACITY_LEVEL_FULL
: The battery is full, battery temperature is
within normal range and adapter power is enough to sustain running background loads.
Android framework can run background tasks without affecting the battery level or
battery performance.
Constant Value: "android.os.extra.CAPACITY_LEVEL"
EXTRA_CHARGING_STATUS
public static final String EXTRA_CHARGING_STATUS
Extra for Intent.ACTION_BATTERY_CHANGED
:
Int value representing the battery charging status.
Constant Value: "android.os.extra.CHARGING_STATUS"
EXTRA_CYCLE_COUNT
public static final String EXTRA_CYCLE_COUNT
Extra for Intent.ACTION_BATTERY_CHANGED
:
Int value representing the battery charging cycle count.
Constant Value: "android.os.extra.CYCLE_COUNT"
EXTRA_HEALTH
public static final String EXTRA_HEALTH
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer containing the current health constant.
Constant Value: "health"
EXTRA_ICON_SMALL
public static final String EXTRA_ICON_SMALL
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer containing the resource ID of a small status bar icon
indicating the current battery state.
Constant Value: "icon-small"
EXTRA_LEVEL
public static final String EXTRA_LEVEL
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer field containing the current battery level, from 0 to
EXTRA_SCALE
.
Constant Value: "level"
EXTRA_PLUGGED
public static final String EXTRA_PLUGGED
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer indicating whether the device is plugged in to a power
source; 0 means it is on battery, other constants are different
types of power sources.
Constant Value: "plugged"
EXTRA_PRESENT
public static final String EXTRA_PRESENT
Extra for Intent.ACTION_BATTERY_CHANGED
:
boolean indicating whether a battery is present.
Constant Value: "present"
EXTRA_SCALE
public static final String EXTRA_SCALE
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer containing the maximum battery level.
Constant Value: "scale"
EXTRA_STATUS
public static final String EXTRA_STATUS
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer containing the current status constant.
Constant Value: "status"
EXTRA_TECHNOLOGY
public static final String EXTRA_TECHNOLOGY
Extra for Intent.ACTION_BATTERY_CHANGED
:
String describing the technology of the current battery.
Constant Value: "technology"
EXTRA_TEMPERATURE
public static final String EXTRA_TEMPERATURE
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer containing the current battery temperature.
Constant Value: "temperature"
EXTRA_VOLTAGE
public static final String EXTRA_VOLTAGE
Extra for Intent.ACTION_BATTERY_CHANGED
:
integer containing the current battery voltage level.
Constant Value: "voltage"
Public methods
computeChargeTimeRemaining
public long computeChargeTimeRemaining ()
Compute an approximation for how much time (in milliseconds) remains until the battery is fully charged. Returns -1 if no time can be computed: either there is not enough current data to make a decision or the battery is currently discharging.
Returns | |
---|---|
long |
how much time is left, in milliseconds, until the battery is fully charged or -1 if the computation fails |
getIntProperty
public int getIntProperty (int id)
Return the value of a battery property of integer type.
Parameters | |
---|---|
id |
int : identifier of the requested property |
Returns | |
---|---|
int |
the property value. If the property is not supported or there is any other error,
return (a) 0 if targetSdkVersion < VERSION_CODES.P or (b) Integer.MIN_VALUE
if targetSdkVersion >= VERSION_CODES.P . |
getLongProperty
public long getLongProperty (int id)
Return the value of a battery property of long type If the platform does not provide the property queried, this value will be Long.MIN_VALUE.
Parameters | |
---|---|
id |
int : identifier of the requested property |
Returns | |
---|---|
long |
the property value, or Long.MIN_VALUE if not supported. |
getStringProperty
public String getStringProperty (int id)
Return the value of a battery property of String type. If the platform does not provide the property queried, this value will be null.
Parameters | |
---|---|
id |
int : identifier of the requested property. |
Returns | |
---|---|
String |
the property value, or null if not supported. |
isCharging
public boolean isCharging ()
Return true if the battery is currently considered to be charging. This means that
the device is plugged in and is supplying sufficient power that the battery level is
going up (or the battery is fully charged). Changes in this state are matched by
broadcasts of ACTION_CHARGING
and ACTION_DISCHARGING
.
Returns | |
---|---|
boolean |