Introduction
Apache Cordova is a platform that can be used to develop mobile applications by using the HTML, CSS and JavaScript. The event is an action that can be detected by the Cordova like the button click event, page onload event etc. In this article I will explain some Battery events, which can fire when the Mobile's Battery can become low or critical. An event is also used to show the status of the Battery. Some of the Battery events are:
Batterycritical
When the battery has reached its critical level then by using this event the Cordova application can detect that the event was generated. This event fires when the Cordova application has found that the percentage of battery power remaining is critical. This event will be called with the object that takes the two properties:
- level : The percentage of battery (0-100). (Number)
- isPlugged : This represents that whether or not the device is plugged in or not.(Boolean)
For this we add an event listener; that is window.addEventListener. After that we receive the Cordova "deviceready" event when the battery is critical:
window.addEventListener("batterycritical", yourCallbackFunction, false);
The platform that support the Batterycritical event are :
- iOS
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
Example
<!DOCTYPE html>
<html>
<head>
<title>Cordova Device Ready Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Call onDeviceReady when Cordova is loaded.
// At this point, the document has loaded but cordova-1.9.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to make calls Cordova methods
function onDeviceReady() {
window.addEventListener("batterycritical", onBatteryCritical, false);
}
// Handle the batterycritical event
function onBatteryCritical(info) {
alert("Battery Level Critical " + info.level + "%\nRecharge Soon!");
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
Batterylow
When the battery has reached its low level then by using this event the Cordova application can detect that the batterylow event was generated. This event is fired when the Cordova application has fond that the percentage of battery power remaining is low. This event will be called with the object that takes the two properties:
- level : The percentage of battery (0-100). (Number)
- isPlugged : This represents that whether or not the device is plugged in or not.(Boolean)
For this we add an event listener; that is window.addEventListener. After that we receive the Cordova "deviceready" event when the battery is critical:
window.addEventListener("batterylow", yourCallbackFunction, false);
The platform that supports the Batterycritical event are :
- iOS
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
Example
<!DOCTYPE html>

Join the conversation! Your thoughts help the community grow.