power_manager — power & battery
Control this app’s power behavior: request exemption from battery optimizations so the process is less likely to be killed in the background. These APIs can increase battery drain.
Table of contents
Interfaces
Functions
Functions
isIgnoringBatteryOptimizations
isIgnoringBatteryOptimizations(pkg?: string): any
Returns whether ignore battery optimizations is enabled for the given package (usually this app).
Example
"nodejs";
const { isIgnoringBatteryOptimizations } = require('power_manager');
console.log('isIgnoringBatteryOptimizations:', isIgnoringBatteryOptimizations());Parameters
| Name | Type | Default | Description |
|---|---|---|---|
pkg? | string | Current app’s packageName | Package to query. |
Returns
any
Whether that package is exempt from battery optimizations (truthy / falsy per runtime).
isScreenOn
isScreenOn(): boolean
true if the device is in an interactive state (awake and able to accept user input—not necessarily that the user is touching it). The primary display is usually on; sensors may briefly turn the screen off while staying “interactive”.
false when the device is dozing or asleep and must wake before interaction. The main display is usually off; features such as ambient display may keep it dimly on.
Example
"nodejs";
const { isScreenOn } = require('power_manager');
console.log(isScreenOn());Returns
boolean
newWakeLock
newWakeLock(levelAndFlags: number, tag: string): WakeLock
Creates a WakeLock with the given level and flags.
levelAndFlags combines one level and optional flags with bitwise OR (|).
Levels (exactly one): PARTIAL_WAKE_LOCK, FULL_WAKE_LOCK, SCREEN_DIM_WAKE_LOCK, SCREEN_BRIGHT_WAKE_LOCK.
Flags (optional, combinable): ACQUIRE_CAUSES_WAKEUP, ON_AFTER_RELEASE.
Call acquire() on the returned object to hold the lock, and release() when done.
Creating the object does not require extra permission, but android.permission.WAKE_LOCK is required to actually acquire / release the lock.
To keep the screen on for a window, prefer FLAG_KEEP_SCREEN_ON: the system manages it across apps and multi-display setups better than holding a full wake lock on every display.
See also
Parameters
| Name | Type | Description |
|---|---|---|
levelAndFlags | number | Wake level plus optional flags (bitmask). |
tag | string | Debug tag (often a class name). |
Returns
requestIgnoreBatteryOptimizations
requestIgnoreBatteryOptimizations(forceRequest?: boolean, pkg?: string): void
Asks the user to disable battery optimizations for pkg. The system shows a confirmation UI; the call is asynchronous and does not report the user’s choice back to the script.
Example
"nodejs";
const { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } = require('power_manager');
if (!isIgnoringBatteryOptimizations()) {
console.log('requestIgnoreBatteryOptimizations');
requestIgnoreBatteryOptimizations();
}Parameters
| Name | Type | Default | Description |
|---|---|---|---|
forceRequest? | boolean | false | If false and the app is already exempt, no new prompt. If true, always attempt the request again. |
pkg? | string | Current packageName | Target package. |
Returns
void
wakeUp
wakeUp(options?: WakeUpOptions): void
Acquires a short-lived wake path that turns the screen on and holds it for a bounded time; the lock is released automatically after timeout (default 5 seconds per Chinese docs).
See also
Example
"nodejs";
const { isScreenOn, wakeUp } = require('power_manager');
if (!isScreenOn()) {
wakeUp();
}Parameters
| Name | Type | Description |
|---|---|---|
options? | WakeUpOptions | Timeout and wake flags. |
Returns
void
