PowerManager
class PowerManager
| kotlin.Any | |
| ↳ | 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 WakeLocks 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 android.view.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.
Summary
Nested classes |
|
|---|---|
| abstract |
Listener passed to |
|
A wake lock is a mechanism to indicate that your application needs to have the device stay on. |
|
Constants |
|
|---|---|
| static Int |
Wake lock flag: Turn the screen on when the wake lock is acquired. |
| static String |
Intent that is broadcast when the state of |
| static String |
Intent that is broadcast when the state of |
| static Int |
Wake lock level: Ensures that the screen and keyboard backlight are on at full brightness. |
| static Int |
All location providers should be disabled when battery saver is on and the device is non-interactive. |
| static Int |
In this mode, all the location providers will be kept available, but location fixes should only be provided to foreground apps. |
| static Int |
In this mode, the GPS based location provider should be disabled when battery saver is on and the device is non-interactive. |
| static Int |
Either the location providers shouldn't be affected by battery saver, or battery saver is off. |
| static Int |
In this mode, location will not be turned off, but LocationManager will throttle all requests to providers when the device is non-interactive. |
| static Int |
Wake lock flag: When this wake lock is released, poke the user activity timer so the screen stays on for a little longer. |
| static Int |
Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off. |
| static Int |
Wake lock level: Turns the screen off when the proximity sensor activates. |
| static Int |
Flag for |
| static Int |
Wake lock level: Ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off. |
| static Int |
Wake lock level: Ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off. |
| static Int |
Thermal status code: Platform has done everything to reduce power. |
| static Int |
Thermal status code: Key components in platform are shutting down due to thermal condition. |
| static Int |
Thermal status code: Light throttling where UX is not impacted. |
| static Int |
Thermal status code: Moderate throttling where UX is not largely impacted. |
| static Int |
Thermal status code: Not under throttling. |
| static Int |
Thermal status code: Severe throttling where UX is largely impacted. |
| static Int |
Thermal status code: Need shutdown immediately. |
Public methods |
|
|---|---|
| Unit |
This function adds a listener for thermal status change, listen call back will be enqueued tasks on the main thread |
| Unit |
addThermalStatusListener(executor: Executor, listener: PowerManager.OnThermalStatusChangedListener)This function adds a listener for thermal status change. |
| Int |
This function returns the current thermal status of the device. |
| Int |
Returns how location features should behave when battery saver is on. |
| Float |
getThermalHeadroom(forecastSeconds: Int)Provides an estimate of how much thermal headroom the device currently has before hitting severe throttling. |
| Boolean |
Returns true if the device is currently in idle mode. |
| Boolean |
isIgnoringBatteryOptimizations(packageName: String!)Return whether the given application package name is on the device's power whitelist. |
| Boolean |
Returns true if the device is in an interactive state. |
| Boolean |
Returns true if the device is currently in power save mode. |
| Boolean |
Returns |
| Boolean |
Returns true if the device is in an interactive state. |
| Boolean |
This function checks if the device has implemented Sustained Performance Mode. |
| Boolean |
isWakeLockLevelSupported(level: Int)Returns true if the specified wake lock level is supported. |
| PowerManager.WakeLock! |
newWakeLock(levelAndFlags: Int, tag: String!)Creates a new wake lock with the specified level and flags. |
| Unit |
Reboot the device. |
| Unit |
This function removes a listener for thermal status change |
Constants
ACQUIRE_CAUSES_WAKEUP
static val ACQUIRE_CAUSES_WAKEUP: Int
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.
Cannot be used with PARTIAL_WAKE_LOCK.
Value: 268435456
ACTION_DEVICE_IDLE_MODE_CHANGED
static val ACTION_DEVICE_IDLE_MODE_CHANGED: String
Intent that is broadcast when the state of isDeviceIdleMode() changes. This broadcast is only sent to registered receivers.
Value: "android.os.action.DEVICE_IDLE_MODE_CHANGED"
ACTION_POWER_SAVE_MODE_CHANGED
static val ACTION_POWER_SAVE_MODE_CHANGED: String
Intent that is broadcast when the state of isPowerSaveMode() changes. This broadcast is only sent to registered receivers.
Value: "android.os.action.POWER_SAVE_MODE_CHANGED"
FULL_WAKE_LOCK
static valFULL_WAKE_LOCK: Int
Deprecated: Most applications should use android.view.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.
Value: 26
LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF
static val LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF: Int
All location providers should be disabled when battery saver is on and the device is non-interactive.
Value: 2
LOCATION_MODE_FOREGROUND_ONLY
static val LOCATION_MODE_FOREGROUND_ONLY: Int
In this mode, all the location providers will be kept available, but location fixes should only be provided to foreground apps.
Value: 3
LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF
static val LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF: Int
In this mode, the GPS based location provider should be disabled when battery saver is on and the device is non-interactive.
Value: 1
LOCATION_MODE_NO_CHANGE
static val LOCATION_MODE_NO_CHANGE: Int
Either the location providers shouldn't be affected by battery saver, or battery saver is off.
Value: 0
LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF
static val LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF: Int
In this mode, location will not be turned off, but LocationManager will throttle all requests to providers when the device is non-interactive.
Value: 4
ON_AFTER_RELEASE
static val ON_AFTER_RELEASE: Int
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.
Value: 536870912
PARTIAL_WAKE_LOCK
static val PARTIAL_WAKE_LOCK: Int
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.
Value: 1
PROXIMITY_SCREEN_OFF_WAKE_LOCK
static val PROXIMITY_SCREEN_OFF_WAKE_LOCK: Int
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 to determine whether this wake lock level is supported.
Cannot be used with ACQUIRE_CAUSES_WAKEUP.
Value: 32
RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY
static val RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY: Int
Flag for WakeLock#release: Defer releasing a PROXIMITY_SCREEN_OFF_WAKE_LOCK wake lock until the proximity sensor indicates that an object is not in close proximity.
Value: 1
SCREEN_BRIGHT_WAKE_LOCK
static valSCREEN_BRIGHT_WAKE_LOCK: Int
Deprecated: Most applications should use android.view.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.
Value: 10
SCREEN_DIM_WAKE_LOCK
static valSCREEN_DIM_WAKE_LOCK: Int
Deprecated: Most applications should use android.view.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.
Value: 6
THERMAL_STATUS_CRITICAL
static val THERMAL_STATUS_CRITICAL: Int
Thermal status code: Platform has done everything to reduce power.
Value: 4
THERMAL_STATUS_EMERGENCY
static val THERMAL_STATUS_EMERGENCY: Int
Thermal status code: Key components in platform are shutting down due to thermal condition. Device functionalities will be limited.
Value: 5
THERMAL_STATUS_LIGHT
static val THERMAL_STATUS_LIGHT: Int
Thermal status code: Light throttling where UX is not impacted.
Value: 1
THERMAL_STATUS_MODERATE
static val THERMAL_STATUS_MODERATE: Int
Thermal status code: Moderate throttling where UX is not largely impacted.
Value: 2
THERMAL_STATUS_NONE
static val THERMAL_STATUS_NONE: Int
Thermal status code: Not under throttling.
Value: 0
THERMAL_STATUS_SEVERE
static val THERMAL_STATUS_SEVERE: Int
Thermal status code: Severe throttling where UX is largely impacted.
Value: 3
THERMAL_STATUS_SHUTDOWN
static val THERMAL_STATUS_SHUTDOWN: Int
Thermal status code: Need shutdown immediately.
Value: 6
Public methods
addThermalStatusListener
fun addThermalStatusListener(listener: PowerManager.OnThermalStatusChangedListener): Unit
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. |
addThermalStatusListener
fun addThermalStatusListener(
executor: Executor,
listener: PowerManager.OnThermalStatusChangedListener
): Unit
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. |
getCurrentThermalStatus
fun getCurrentThermalStatus(): Int
This function returns the current thermal status of the device.
| Return | |
|---|---|
Int |
thermal status as int, THERMAL_STATUS_NONE if device in not under thermal throttling. Value is android.os.PowerManager#THERMAL_STATUS_NONE, android.os.PowerManager#THERMAL_STATUS_LIGHT, android.os.PowerManager#THERMAL_STATUS_MODERATE, android.os.PowerManager#THERMAL_STATUS_SEVERE, android.os.PowerManager#THERMAL_STATUS_CRITICAL, android.os.PowerManager#THERMAL_STATUS_EMERGENCY, or android.os.PowerManager#THERMAL_STATUS_SHUTDOWN |
getLocationPowerSaveMode
fun getLocationPowerSaveMode(): Int
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.
getThermalHeadroom
fun getThermalHeadroom(forecastSeconds: Int): Float
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 |
| Return | |
|---|---|
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. |
isDeviceIdleMode
fun isDeviceIdleMode(): Boolean
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.
| Return | |
|---|---|
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. |
isIgnoringBatteryOptimizations
fun isIgnoringBatteryOptimizations(packageName: String!): Boolean
Return whether the given application package name is on the device's power whitelist. Apps can be placed on the whitelist through the settings UI invoked by android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS.
isInteractive
fun isInteractive(): Boolean
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.
| Return | |
|---|---|
Boolean |
True if the device is in an interactive state. |
isPowerSaveMode
fun isPowerSaveMode(): Boolean
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.
| Return | |
|---|---|
Boolean |
Returns true if currently in low power mode, else false. |
isRebootingUserspaceSupported
fun isRebootingUserspaceSupported(): Boolean
Returns true if this device supports rebooting userspace.
isScreenOn
funisScreenOn(): Boolean
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 android.view.Display#getState.
| Return | |
|---|---|
Boolean |
True if the device is in an interactive state. |
isSustainedPerformanceModeSupported
fun isSustainedPerformanceModeSupported(): Boolean
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 android.view.Window#setSustainedPerformanceMode.
| Return | |
|---|---|
Boolean |
Returns True if the device supports it, false otherwise. |
isWakeLockLevelSupported
fun isWakeLockLevelSupported(level: Int): Boolean
Returns true if the specified wake lock level is supported.
| Parameters | |
|---|---|
level |
Int: The wake lock level to check. |
| Return | |
|---|---|
Boolean |
True if the specified wake lock level is supported. |
newWakeLock
fun newWakeLock(
levelAndFlags: Int,
tag: String!
): PowerManager.WakeLock!
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 WakeLock#acquire() on the object to acquire the wake lock, and WakeLock#release when you are done.
{@samplecode * PowerManager pm = (PowerManager)mContext.getSystemService( * Context.POWER_SERVICE); * 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 android.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 android.view.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.
Recommended naming conventions for tags to make debugging easier:
- use a unique prefix delimited by a colon for your app/library (e.g. gmail:mytag) to make it easier to understand where the wake locks comes from. This namespace will also avoid collision for tags inside your app coming from different libraries which will make debugging easier.
- use constants (e.g. do not include timestamps in the tag) to make it easier for tools to aggregate similar wake locks. When collecting debugging data, the platform only monitors a finite number of tags, using constants will help tools to provide better debugging data.
- avoid using Class#getName() or similar method since this class name can be transformed by java optimizer and obfuscator tools.
- avoid wrapping the tag or a prefix to avoid collision with wake lock tags from the platform (e.g. *alarm*).
- never include personnally identifiable information for privacy reasons.
| 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. |
reboot
fun reboot(reason: String?): Unit
Reboot the device. Will not return if the reboot is successful.
Requires the android.Manifest.permission#REBOOT permission.
Requires
android.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. |
| Exceptions | |
|---|---|
java.lang.UnsupportedOperationException |
if userspace reboot was requested on a device that doesn't support it. |
removeThermalStatusListener
fun removeThermalStatusListener(listener: PowerManager.OnThermalStatusChangedListener): Unit
This function removes a listener for thermal status change
| Parameters | |
|---|---|
listener |
PowerManager.OnThermalStatusChangedListener: listener to be removed This value cannot be null. |