power_manager
CloudControl Pro 9 Docs / power_manager
power_manager
This module can let you control the power policy of this application, by ignoring battery optimization to let this application run in background. Using this API may cause the battery life of the device to be affected.
Table of contents
Interfaces
Functions
Functions
isIgnoringBatteryOptimizations
▸ isIgnoringBatteryOptimizations(pkg?
): any
Returns whether the application with the specify package name is enabled to ignore battery optimization.
Example
"nodejs";
const { isIgnoringBatteryOptimizations } = require('power_manager');
console.log('isIgnoringBatteryOptimizations:', isIgnoringBatteryOptimizations());
Parameters
Name | Type | Default value | Description |
---|---|---|---|
pkg |
string |
packageName |
The package name of the application, default to the package name of this application. |
Returns
any
Whether the application is enabled to ignore battery optimization.
isScreenOn
▸ isScreenOn(): 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.
Example
"nodejs";
const { isScreenOn } = require('power_manager');
console.log(isScreenOn());
Returns
boolean
newWakeLock
▸ newWakeLock(levelAndFlags
, tag
): 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 acquire() on the object to acquire the wake lock, and release() when you are done.
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. 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.
See
Parameters
Name | Type | Description |
---|---|---|
levelAndFlags |
number |
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
requestIgnoreBatteryOptimizations
▸ requestIgnoreBatteryOptimizations(forceRequest?
, pkg?
): void
Request user to ignore battery optimization of the application with the specify package name. System will pop up a dialog to ask user to confirm, this process is asynchronous, the result of the confirmation will not be returned.
Example
"nodejs";
const { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } = require('power_manager');
if (!isIgnoringBatteryOptimizations()) {
console.log('requestIgnoreBatteryOptimizations');
requestIgnoreBatteryOptimizations();
}
Parameters
Name | Type | Default value | Description |
---|---|---|---|
forceRequest |
boolean |
false |
If false and the current application is enabled to ignore battery optimization, the request will not be executed; if true, the request will always be executed. The default is false. |
pkg |
string |
packageName |
The package name of the application to ignore battery optimization. The default is the package name of this application. |
Returns
void
wakeUp
▸ wakeUp(options?
): void
Acquire a wakelock that wakes the screen for a certain amount of time.
See
Example
"nodejs";
const { isScreenOn, wakeUp } = require('power_manager');
if (!isScreenOn()) {
wakeUp();
}
Parameters
Name | Type |
---|---|
options? |
WakeUpOptions |
Returns
void