DedrisFrameworkDedrisFramework

← Plugins  /  Battery Status

🔋

Battery Status

cordova-plugin-battery-status
iOS Android 🌐 Browser

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.

Installation

Add the plugin to your Cordova project from the command line:

terminal
cordova plugin add cordova-plugin-battery-status

Events

The plugin emits three window events. Add listeners after the deviceready event has fired:

EventFires when
batterystatusThe charge level changes by at least one percent, or the device is plugged in or unplugged.
batterylowThe charge reaches the platform's low-battery threshold.
batterycriticalThe charge reaches the platform's critical-battery threshold.

The status object

Each event handler receives a status object with two properties:

PropertyTypeDescription
levelNumberThe remaining charge as a percentage, from 0 to 100.
isPluggedBooleanWhether the device is currently connected to power.

Usage example

Listen for battery updates and read the level and charging state:

www/js/app.js
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);
}

Tips

📚 Source: the technical reference on this page (events, properties and the install command) is based on the official Apache Cordova battery-status documentation. Descriptions and examples have been rewritten for DedrisFramework. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project