← Plugins / Battery Status
This official Apache Cordova plugin lets your app react to changes in the device's battery. It reports the current charge level and whether the device is charging, and fires events as the battery drains or is plugged in — handy for power-aware features like pausing background work when the battery runs low.
Add the plugin to your Cordova project from the command line:
cordova plugin add cordova-plugin-battery-status
The plugin emits three window events. Add listeners after the deviceready event has fired:
| Event | Fires when |
|---|---|
batterystatus | The charge level changes by at least one percent, or the device is plugged in or unplugged. |
batterylow | The charge reaches the platform's low-battery threshold. |
batterycritical | The charge reaches the platform's critical-battery threshold. |
Each event handler receives a status object with two properties:
| Property | Type | Description |
|---|---|---|
level | Number | The remaining charge as a percentage, from 0 to 100. |
isPlugged | Boolean | Whether the device is currently connected to power. |
Listen for battery updates and read the level and charging state:
document.addEventListener("deviceready", onReady, false);
function onReady() {
window.addEventListener("batterystatus", onBatteryStatus, false);
}
function onBatteryStatus(status) {
console.log("Level: " + status.level + "%");
console.log("Charging: " + status.isPlugged);
}
deviceready — the plugin isn't available before then.batterylow and batterycritical to scale back battery-hungry work automatically.window.removeEventListener when a screen is torn down to avoid duplicate handlers.