Cordova - Battery Status




Cordova - Battery Status

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:\Users\username\Desktop\CordovaProject>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%.

Cordova - Battery Status

If we plug in the device to the charger, the new alert will show that the isPlugged value is changed to true.

Cordova - Battery Status

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.



Frequently Asked Questions

+
Ans: Cordova - Plugman view more..
+
Ans: Cordova - Back Button view more..
+
Ans: Cordova - Events view more..
+
Ans: Cordova - Battery Status view more..
+
Ans: Cordova - Camera view more..
+
Ans: Cordova - Contacts view more..
+
Ans: Cordova - Device view more..
+
Ans: Cordova - Accelerometer view more..
+
Ans: Cordova - Device Orientation view more..
+
Ans: Cordova - Dialogs view more..
+
Ans: Cordova - File System view more..
+
Ans: Cordova - File Transfer view more..
+
Ans: Cordova - Geolocation view more..
+
Ans: Cordova - Globalization view more..
+
Ans: Cordova - InAppBrowser view more..
+
Ans: Cordova - Media view more..
+
Ans: Cordova - Media Capture view more..
+
Ans: Cordova - Network Information view more..




Rating - NAN/5
547 views

Advertisements