# Print output for @column tags ?>
public
final
class
PowerManager
extends Object
java.lang.Object | |
↳ | android.os.PowerManager |
This class gives you control of the power state of the device.
Device battery life will be significantly affected by the use of this API.
Do not acquire WakeLock
s unless you really need them, use the minimum levels
possible, and be sure to release them as soon as possible. In most cases,
you'll want to use
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
instead.
Any application using a WakeLock must request the android.permission.WAKE_LOCK
permission in an <uses-permission>
element of the application's manifest.
Nested classes | |
---|---|
interface |
PowerManager.OnThermalStatusChangedListener
Listener passed to
|
class |
PowerManager.WakeLock
A wake lock is a mechanism to indicate that your application needs to have the device stay on. |
Constants | |
---|---|
int |
ACQUIRE_CAUSES_WAKEUP
Wake lock flag: Turn the screen on when the wake lock is acquired. |
String |
ACTION_DEVICE_IDLE_MODE_CHANGED
Intent that is broadcast when the state of |
String |
ACTION_POWER_SAVE_MODE_CHANGED
Intent that is broadcast when the state of |
int |
FULL_WAKE_LOCK
This constant is deprecated.
Most applications should use
|
int |
LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF
All location providers should be disabled when battery saver is on and the device is non-interactive. |
int |
LOCATION_MODE_FOREGROUND_ONLY
In this mode, all the location providers will be kept available, but location fixes should only be provided to foreground apps. |
int |
LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF
In this mode, the GPS based location provider should be disabled when battery saver is on and the device is non-interactive. |
int |
LOCATION_MODE_NO_CHANGE
Either the location providers shouldn't be affected by battery saver, or battery saver is off. |
int |
LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF
In this mode, location will not be turned off, but LocationManager will throttle all requests to providers when the device is non-interactive. |
int |
ON_AFTER_RELEASE
Wake lock flag: When this wake lock is released, poke the user activity timer so the screen stays on for a little longer. |
int |
PARTIAL_WAKE_LOCK
Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off. |
int |
PROXIMITY_SCREEN_OFF_WAKE_LOCK
Wake lock level: Turns the screen off when the proximity sensor activates. |
int |
RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY
Flag for |
int |
SCREEN_BRIGHT_WAKE_LOCK
This constant is deprecated.
Most applications should use
|
int |
SCREEN_DIM_WAKE_LOCK
This constant is deprecated.
Most applications should use
|
int |
THERMAL_STATUS_CRITICAL
Thermal status code: Platform has done everything to reduce power. |
int |
THERMAL_STATUS_EMERGENCY
Thermal status code: Key components in platform are shutting down due to thermal condition. |
int |
THERMAL_STATUS_LIGHT
Thermal status code: Light throttling where UX is not impacted. |
int |
THERMAL_STATUS_MODERATE
Thermal status code: Moderate throttling where UX is not largely impacted. |
int |
THERMAL_STATUS_NONE
Thermal status code: Not under throttling. |
int |
THERMAL_STATUS_SEVERE
Thermal status code: Severe throttling where UX is largely impacted. |
int |
THERMAL_STATUS_SHUTDOWN
Thermal status code: Need shutdown immediately. |
Public methods | |
---|---|
void
|
addThermalStatusListener(Executor executor, PowerManager.OnThermalStatusChangedListener listener)
This function adds a listener for thermal status change. |
void
|
addThermalStatusListener(PowerManager.OnThermalStatusChangedListener listener)
This function adds a listener for thermal status change, listen call back will be enqueued tasks on the main thread |
Duration
|
getBatteryDischargePrediction()
Returns the current battery life remaining estimate. |
int
|
getCurrentThermalStatus()
This function returns the current thermal status of the device. |
int
|
getLocationPowerSaveMode()
Returns how location features should behave when battery saver is on. |
float
|
getThermalHeadroom(int forecastSeconds)
Provides an estimate of how much thermal headroom the device currently has before hitting severe throttling. |
boolean
|
isBatteryDischargePredictionPersonalized()
Returns whether the current battery life remaining estimate is personalized based on device usage history or not. |
boolean
|
isDeviceIdleMode()
Returns true if the device is currently in idle mode. |
boolean
|
isIgnoringBatteryOptimizations(String packageName)
Return whether the given application package name is on the device's power allowlist. |
boolean
|
isInteractive()
Returns true if the device is in an interactive state. |
boolean
|
isPowerSaveMode()
Returns true if the device is currently in power save mode. |
boolean
|
isRebootingUserspaceSupported()
Returns |
boolean
|
isScreenOn()
This method is deprecated.
Use |
boolean
|
isSustainedPerformanceModeSupported()
This function checks if the device has implemented Sustained Performance Mode. |
boolean
|
isWakeLockLevelSupported(int level)
Returns true if the specified wake lock level is supported. |
PowerManager.WakeLock
|
newWakeLock(int levelAndFlags, String tag)
Creates a new wake lock with the specified level and flags. |
void
|
reboot(String reason)
Reboot the device. |
void
|
removeThermalStatusListener(PowerManager.OnThermalStatusChangedListener listener)
This function removes a listener for thermal status change |
Inherited methods | |
---|---|
public static final int ACQUIRE_CAUSES_WAKEUP
Wake lock flag: Turn the screen on when the wake lock is acquired.
Normally wake locks don't actually wake the device, they just cause the screen to remain on once it's already on. Think of the video player application as the normal behavior. Notifications that pop up and want the device to be on are the exception; use this flag to be like them.
Android TV playback devices attempt to turn on the HDMI-connected TV via HDMI-CEC on any wake-up, including wake-ups triggered by wake locks.
Cannot be used with PARTIAL_WAKE_LOCK
.
Constant Value: 268435456 (0x10000000)
public static final String ACTION_DEVICE_IDLE_MODE_CHANGED
Intent that is broadcast when the state of isDeviceIdleMode()
changes.
This broadcast is only sent to registered receivers.
Constant Value: "android.os.action.DEVICE_IDLE_MODE_CHANGED"
public static final String ACTION_POWER_SAVE_MODE_CHANGED
Intent that is broadcast when the state of isPowerSaveMode()
changes.
This broadcast is only sent to registered receivers.
Constant Value: "android.os.action.POWER_SAVE_MODE_CHANGED"
public static final int FULL_WAKE_LOCK
This constant is deprecated.
Most applications should use
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
instead
of this type of wake lock, as it will be correctly managed by the platform
as the user moves between applications and doesn't require a special permission.
Wake lock level: Ensures that the screen and keyboard backlight are on at full brightness.
If the user presses the power button, then the FULL_WAKE_LOCK
will be
implicitly released by the system, causing both the screen and the CPU to be turned off.
Contrast with PARTIAL_WAKE_LOCK
.
Constant Value: 26 (0x0000001a)
public static final int LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF
All location providers should be disabled when battery saver is on and the device is non-interactive.
Constant Value: 2 (0x00000002)
public static final int LOCATION_MODE_FOREGROUND_ONLY
In this mode, all the location providers will be kept available, but location fixes should only be provided to foreground apps.
Constant Value: 3 (0x00000003)
public static final int LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF
In this mode, the GPS based location provider should be disabled when battery saver is on and the device is non-interactive.
Constant Value: 1 (0x00000001)
public static final int LOCATION_MODE_NO_CHANGE
Either the location providers shouldn't be affected by battery saver, or battery saver is off.
Constant Value: 0 (0x00000000)
public static final int LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF
In this mode, location will not be turned off, but LocationManager will throttle all requests to providers when the device is non-interactive.
Constant Value: 4 (0x00000004)
public static final int ON_AFTER_RELEASE
Wake lock flag: When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.
Will not turn the screen on if it is not already on.
See ACQUIRE_CAUSES_WAKEUP
if you want that.
Cannot be used with PARTIAL_WAKE_LOCK
.
Constant Value: 536870912 (0x20000000)
public static final int PARTIAL_WAKE_LOCK
Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.
If the user presses the power button, then the screen will be turned off but the CPU will be kept on until all partial wake locks have been released.
Constant Value: 1 (0x00000001)
public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK
Wake lock level: Turns the screen off when the proximity sensor activates.
If the proximity sensor detects that an object is nearby, the screen turns off immediately. Shortly after the object moves away, the screen turns on again.
A proximity wake lock does not prevent the device from falling asleep
unlike FULL_WAKE_LOCK
, SCREEN_BRIGHT_WAKE_LOCK
and
SCREEN_DIM_WAKE_LOCK
. If there is no user activity and no other
wake locks are held, then the device will fall asleep (and lock) as usual.
However, the device will not fall asleep while the screen has been turned off
by the proximity sensor because it effectively counts as ongoing user activity.
Since not all devices have proximity sensors, use isWakeLockLevelSupported(int)
to determine whether this wake lock level is supported.
Cannot be used with ACQUIRE_CAUSES_WAKEUP
.
Constant Value: 32 (0x00000020)
public static final int RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY
Flag for WakeLock.release(int)
: Defer releasing a
PROXIMITY_SCREEN_OFF_WAKE_LOCK
wake lock until the proximity sensor
indicates that an object is not in close proximity.
Constant Value: 1 (0x00000001)
public static final int SCREEN_BRIGHT_WAKE_LOCK
This constant is deprecated.
Most applications should use
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
instead
of this type of wake lock, as it will be correctly managed by the platform
as the user moves between applications and doesn't require a special permission.
Wake lock level: Ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off.
If the user presses the power button, then the SCREEN_BRIGHT_WAKE_LOCK
will be
implicitly released by the system, causing both the screen and the CPU to be turned off.
Contrast with PARTIAL_WAKE_LOCK
.
Constant Value: 10 (0x0000000a)
public static final int SCREEN_DIM_WAKE_LOCK
This constant is deprecated.
Most applications should use
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
instead
of this type of wake lock, as it will be correctly managed by the platform
as the user moves between applications and doesn't require a special permission.
Wake lock level: Ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off.
If the user presses the power button, then the SCREEN_DIM_WAKE_LOCK
will be
implicitly released by the system, causing both the screen and the CPU to be turned off.
Contrast with PARTIAL_WAKE_LOCK
.
Constant Value: 6 (0x00000006)
public static final int THERMAL_STATUS_CRITICAL
Thermal status code: Platform has done everything to reduce power.
Constant Value: 4 (0x00000004)
public static final int THERMAL_STATUS_EMERGENCY
Thermal status code: Key components in platform are shutting down due to thermal condition. Device functionalities will be limited.
Constant Value: 5 (0x00000005)
public static final int THERMAL_STATUS_LIGHT
Thermal status code: Light throttling where UX is not impacted.
Constant Value: 1 (0x00000001)
public static final int THERMAL_STATUS_MODERATE
Thermal status code: Moderate throttling where UX is not largely impacted.
Constant Value: 2 (0x00000002)
public static final int THERMAL_STATUS_NONE
Thermal status code: Not under throttling.
Constant Value: 0 (0x00000000)
public static final int THERMAL_STATUS_SEVERE
Thermal status code: Severe throttling where UX is largely impacted.
Constant Value: 3 (0x00000003)
public static final int THERMAL_STATUS_SHUTDOWN
Thermal status code: Need shutdown immediately.
Constant Value: 6 (0x00000006)
public void addThermalStatusListener (Executor executor, PowerManager.OnThermalStatusChangedListener listener)
This function adds a listener for thermal status change.
Parameters | |
---|---|
executor |
Executor : Executor to handle listener callback.
This value cannot be null .
Callback and listener events are dispatched through this
Executor , providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor() .
To dispatch events through a shared thread pool, you can use
AsyncTask#THREAD_POOL_EXECUTOR . |
listener |
PowerManager.OnThermalStatusChangedListener : listener to be added.
This value cannot be null . |
public void addThermalStatusListener (PowerManager.OnThermalStatusChangedListener listener)
This function adds a listener for thermal status change, listen call back will be enqueued tasks on the main thread
Parameters | |
---|---|
listener |
PowerManager.OnThermalStatusChangedListener : listener to be added,
This value cannot be null . |
public Duration getBatteryDischargePrediction ()
Returns the current battery life remaining estimate.
Returns | |
---|---|
Duration |
The estimated battery life remaining as a Duration . Will be null if
the device is powered, charging, or an error was encountered. |
public int getCurrentThermalStatus ()
This function returns the current thermal status of the device.
Returns | |
---|---|
int |
thermal status as int, THERMAL_STATUS_NONE if device in not under
thermal throttling.
Value is THERMAL_STATUS_NONE , THERMAL_STATUS_LIGHT , THERMAL_STATUS_MODERATE , THERMAL_STATUS_SEVERE , THERMAL_STATUS_CRITICAL , THERMAL_STATUS_EMERGENCY , or THERMAL_STATUS_SHUTDOWN |
public int getLocationPowerSaveMode ()
Returns how location features should behave when battery saver is on. When battery saver
is off, this will always return LOCATION_MODE_NO_CHANGE
.
This API is normally only useful for components that provide location features.
public float getThermalHeadroom (int forecastSeconds)
Provides an estimate of how much thermal headroom the device currently has before hitting
severe throttling.
Note that this only attempts to track the headroom of slow-moving sensors, such as the skin
temperature sensor. This means that there is no benefit to calling this function more
frequently than about once per second, and attempts to call significantly more frequently may
result in the function returning NaN
.
In addition, in order to be able to provide an accurate forecast, the system does not attempt
to forecast until it has multiple temperature samples from which to extrapolate. This should
only take a few seconds from the time of the first call, but during this time, no forecasting
will occur, and the current headroom will be returned regardless of the value of
forecastSeconds
.
The value returned is a non-negative float that represents how much of the thermal envelope
is in use (or is forecasted to be in use). A value of 1.0 indicates that the device is (or
will be) throttled at THERMAL_STATUS_SEVERE
. Such throttling can affect the CPU,
GPU, and other subsystems. Values may exceed 1.0, but there is no implied mapping to specific
thermal status levels beyond that point. This means that values greater than 1.0 may
correspond to THERMAL_STATUS_SEVERE
, but may also represent heavier throttling.
A value of 0.0 corresponds to a fixed distance from 1.0, but does not correspond to any particular thermal status or temperature. Values on (0.0, 1.0] may be expected to scale linearly with temperature, though temperature changes over time are typically not linear. Negative values will be clamped to 0.0 before returning.
Parameters | |
---|---|
forecastSeconds |
int : how many seconds in the future to forecast. Given that device
conditions may change at any time, forecasts from further in the
future will likely be less accurate than forecasts in the near future.
Value is between 0 and 60 inclusive |
Returns | |
---|---|
float |
a value greater than or equal to 0.0 where 1.0 indicates the SEVERE throttling threshold, as described above. Returns NaN if the device does not support this functionality or if this function is called significantly faster than once per second. |
public boolean isBatteryDischargePredictionPersonalized ()
Returns whether the current battery life remaining estimate is personalized based on device usage history or not. This value does not take a device's powered or charging state into account.
Returns | |
---|---|
boolean |
A boolean indicating if the current discharge estimate is personalized based on historical device usage or not. |
public boolean isDeviceIdleMode ()
Returns true if the device is currently in idle mode. This happens when a device
has been sitting unused and unmoving for a sufficiently long period of time, so that
it decides to go into a lower power-use state. This may involve things like turning
off network access to apps. You can monitor for changes to this state with
ACTION_DEVICE_IDLE_MODE_CHANGED
.
Returns | |
---|---|
boolean |
Returns true if currently in active device idle mode, else false. This is when idle mode restrictions are being actively applied; it will return false if the device is in a long-term idle mode but currently running a maintenance window where restrictions have been lifted. |
public boolean isIgnoringBatteryOptimizations (String packageName)
Return whether the given application package name is on the device's power allowlist.
Apps can be placed on the allowlist through the settings UI invoked by
Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
.
Being on the power allowlist means that the system will not apply most power saving features to the app. Guardrails for extreme cases may still be applied.
Parameters | |
---|---|
packageName |
String |
Returns | |
---|---|
boolean |
public boolean isInteractive ()
Returns true if the device is in an interactive state.
When this method returns true, the device is awake and ready to interact with the user (although this is not a guarantee that the user is actively interacting with the device just this moment). The main screen is usually turned on while in this state. Certain features, such as the proximity sensor, may temporarily turn off the screen while still leaving the device in an interactive state. Note in particular that the device is still considered to be interactive while dreaming (since dreams can be interactive) but not when it is dozing or asleep.
When this method returns false, the device is dozing or asleep and must be awoken before it will become ready to interact with the user again. The main screen is usually turned off while in this state. Certain features, such as "ambient mode" may cause the main screen to remain on (albeit in a low power state) to display system-provided content while the device dozes.
The system will send a screen on
or screen off
broadcast
whenever the interactive state of the device changes. For historical reasons,
the names of these broadcasts refer to the power state of the screen
but they are actually sent in response to changes in the overall interactive
state of the device, as described by this method.
Services may use the non-interactive state as a hint to conserve power since the user is not present.
Returns | |
---|---|
boolean |
True if the device is in an interactive state. |
public boolean isPowerSaveMode ()
Returns true if the device is currently in power save mode. When in this mode,
applications should reduce their functionality in order to conserve battery as
much as possible. You can monitor for changes to this state with
ACTION_POWER_SAVE_MODE_CHANGED
.
Returns | |
---|---|
boolean |
Returns true if currently in low power mode, else false. |
public boolean isRebootingUserspaceSupported ()
Returns true
if this device supports rebooting userspace.
Returns | |
---|---|
boolean |
public boolean isScreenOn ()
This method is deprecated.
Use isInteractive()
instead.
Returns true if the device is in an interactive state.
For historical reasons, the name of this method refers to the power state of
the screen but it actually describes the overall interactive state of
the device. This method has been replaced by isInteractive()
.
The value returned by this method only indicates whether the device is
in an interactive state which may have nothing to do with the screen being
on or off. To determine the actual state of the screen,
use Display.getState()
.
Returns | |
---|---|
boolean |
True if the device is in an interactive state. |
public boolean isSustainedPerformanceModeSupported ()
This function checks if the device has implemented Sustained Performance
Mode. This needs to be checked only once and is constant for a particular
device/release.
Sustained Performance Mode is intended to provide a consistent level of
performance for prolonged amount of time.
Applications should check if the device supports this mode, before using
Window.setSustainedPerformanceMode(boolean)
.
Returns | |
---|---|
boolean |
Returns True if the device supports it, false otherwise. |
public boolean isWakeLockLevelSupported (int level)
Returns true if the specified wake lock level is supported.
Parameters | |
---|---|
level |
int : The wake lock level to check. |
Returns | |
---|---|
boolean |
True if the specified wake lock level is supported. |
public PowerManager.WakeLock newWakeLock (int levelAndFlags, String tag)
Creates a new wake lock with the specified level and flags.
The levelAndFlags
parameter specifies a wake lock level and optional flags
combined using the logical OR operator.
The wake lock levels are: PARTIAL_WAKE_LOCK
,
FULL_WAKE_LOCK
, SCREEN_DIM_WAKE_LOCK
and SCREEN_BRIGHT_WAKE_LOCK
. Exactly one wake lock level must be
specified as part of the levelAndFlags
parameter.
The wake lock flags are: ACQUIRE_CAUSES_WAKEUP
and ON_AFTER_RELEASE
. Multiple flags can be combined as part of the
levelAndFlags
parameters.
Call acquire()
on the object to acquire the
wake lock, and release()
when you are done.
PowerManager pm = mContext.getSystemService(PowerManager.class); PowerManager.WakeLock wl = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); wl.acquire(); // ... do work... wl.release();
Although a wake lock can be created without special permissions,
the Manifest.permission.WAKE_LOCK
permission is
required to actually acquire or release the wake lock that is returned.
If using this to keep the screen on, you should strongly consider using
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
instead.
This window flag will be correctly managed by the platform
as the user moves between applications and doesn't require a special permission.
Additionally using the flag will keep only the appropriate screen on in a
multi-display scenario while using a wake lock will keep every screen powered on.
Recommended naming conventions for tags to make debugging easier:
Parameters | |
---|---|
levelAndFlags |
int : Combination of wake lock level and flag values defining
the requested behavior of the WakeLock. |
tag |
String : Your class name (or other tag) for debugging purposes. |
Returns | |
---|---|
PowerManager.WakeLock |
public void reboot (String reason)
Reboot the device. Will not return if the reboot is successful.
Requires the Manifest.permission.REBOOT
permission.
If the reason
string contains ",quiescent", then the screen stays off during reboot
and is not turned on again until the user triggers the device to wake up (for example,
by pressing the power key).
This behavior applies to Android TV devices launched on Android 11 (API level 30) or higher.
Manifest.permission.REBOOT
Parameters | |
---|---|
reason |
String : code to pass to the kernel (e.g., "recovery") to
request special boot modes, or null.
This value may be null . |
Throws | |
---|---|
UnsupportedOperationException |
if userspace reboot was requested on a device that doesn't support it. |
public void removeThermalStatusListener (PowerManager.OnThermalStatusChangedListener listener)
This function removes a listener for thermal status change
Parameters | |
---|---|
listener |
PowerManager.OnThermalStatusChangedListener : listener to be removed
This value cannot be null . |