---
title: "Cordova - Battery Plugin"  
description: "This Cordova plugin allows monitoring of battery status of the device. It will monitor and show each change that occurs when the battery is being used"  
author: "Anonymous User"  
published: 2017-04-20  
updated: 2018-03-19  
canonical: https://www.mindstick.com/blog/11357/cordova-battery-plugin  
category: "apache cordova"  
tags: ["mobile development", "apache", "cordova"]  
reading_time: 2 minutes  

---

# Cordova - Battery Plugin

In the series of [learning](https://www.mindstick.com/articles/126221/instructional-design-for-elearning-why-it-is-so-important) [Apache Cordova](https://www.mindstick.com/blog/11351/mobile-application-development-using-apache-cordova), This article will tell you about how to install and use Battery plugin in [your app](https://answers.mindstick.com/qa/33368/how-will-you-design-a-listview-which-downloads-a-lot-of-images-and-displays-in-it-without-getting-your-app-hanged-whats-the-best-possible-way).

This Cordova plugin allows [monitoring](https://answers.mindstick.com/qa/102711/explain-the-basics-of-google-s-cloud-monitoring-for-tracking-performance-metrics) of battery status of the device. It will monitor and show each change that occurs when the battery is being used.

**Step 1 - Battery Plugin [Installation](https://www.mindstick.com/articles/711/php-installation-on-windows)**

To install the battery plugin, open **[command prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt)** window and execute the following code.

```
C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-battery-status  
```

##

##### Step 2 - Adding Event Listener

There is an **onDeviceReady** function inside the index.js file. This is where the [event listener](https://www.mindstick.com/interview/529/how-to-remove-the-event-listener) is to be added.

```
window.addEventListener("batterystatus", onBatteryStatus, false);
```

**Step 3 - Create [Callback Function](https://www.mindstick.com/forum/157808/what-is-a-callback-function-and-how-is-it-used-in-javascript)**

Now, you have to create **onBatteryStatus** callback function at the bottom of index.js file.

```
function onBatteryStatus(info){
   alert("BATTERY STATUS:  Level: " + info.level + " isPlugged: " + info.isPlugged);
}
```

On running the app, alert will be triggered, when the battery is fully charged to 100%.

Once the battery status is changed, the new alert will be displayed. The battery status is now changes to 99%.

![Cordova - Battery Plugin](https://www.mindstick.com/blogs/a99491ad-e276-46b6-9c3e-be368a50cf88/images/14d3722c-9c65-4aed-978e-d4566ef98fdb.png)\

Then, when you plug in the charger to the device, the new alert will display **isPlugged** value is set to true.\

\

![Cordova - Battery Plugin](https://www.mindstick.com/blogs/a99491ad-e276-46b6-9c3e-be368a50cf88/images/91609dcf-3608-4a72-9f57-b258b9c46681.png)\

**Additional Events**

This plugin also have additional two events besides **batterystatus.** These events can also be used in the same way as batterystatus event.

| ## Event | ## Details |
| --- | --- |
| batterylow | This event is launched when battery charge percentage reached to a lower value. This value varies on different devices. |
| batterycritical | This event is launched when battery charge percentage reached to a [critical](https://www.mindstick.com/blog/11798/4-critical-factors-to-weigh-in-before-finalizing-a-med-school) value. This value also varies on different devices. |

**Also Read Previous Post: [Event Handling In Apache Cordova](https://www.mindstick.com/blog/11355/handling-events-in-app-built-using-apache-cordova)**\

---

Original Source: https://www.mindstick.com/blog/11357/cordova-battery-plugin

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
