”;
This Cordova plugin is used for monitoring device”s battery status. The plugin will monitor every change that happens to device”s battery.
Step 1 – Installing Battery Plugin
To install this plugin, we need to open the command prompt window and run the following code.
C:UsersusernameDesktopCordovaProject>cordova plugin add cordova-pluginbattery-status
Step 2 – Add Event Listener
When you open the index.js file, you will find the onDeviceReady function. This is where the event listener should be added.
window.addEventListener("batterystatus", onBatteryStatus, false);
Step 3 – Create Callback Function
We will create the onBatteryStatus callback function at the bottom of the index.js file.
function onBatteryStatus(info) { alert("BATTERY STATUS: Level: " + info.level + " isPlugged: " + info.isPlugged); }
When we run the app, an alert will be triggered. At the moment, the battery is 100% charged.
When the status is changed, a new alert will be displayed. The battery status shows that the battery is now charged 99%.
If we plug in the device to the charger, the new alert will show that the isPlugged value is changed to true.
Additional Events
This plugin offers two additional events besides the batterystatus event. These events can be used in the same way as the batterystatus event.
S.No | Event & Details |
---|---|
1 |
batterylow The event is triggered when the battery charge percentage reaches low value. This value varies with different devices. |
2 |
batterycritical The event is triggered when the battery charge percentage reaches critical value. This value varies with different devices. |
”;