Exploring Android’s BatteryManager API

M Farhan Majid
3 min readJun 26, 2021

Let’s take a look at one of the oldest APIs in Android’s library: BatteryManager. With this API, you can peek through user’s various battery properties at any given time.

We will create a simple Android application that displays list of user’s battery properties.

Where’s the source code?


With BatteryManager API, you can obtain various properties in regard to user device’s battery. Look at the image below to see all of the available properties that you can get:

Various battery properties that you can get from BatteryManager API.

Here’s the complete list of properties that you can get from BatteryManager API:

  • Health: Current battery health (Cold, Dead, Good, Overheat, Over Voltage, Unknown, or Unspecified Failure)
  • Status: Current battery status (Charging, Discharging, Full, Not Charging, or Unknown)
  • Level: Current battery level, from 0 to Scale (see next point)
  • Scale: Maximum battery level (usually 100)
  • Plugged: Which power source the battery is plugged in to (AC, USD, Wireless)
  • Present: Whether battery is present
  • Technology: The technology of the current battery
  • Battery Low: Whether battery is currently considered to be low
  • Voltage: Current battery voltage level
  • Temperature: Current battery temperature
  • Capacity: Remaining battery capacity (the same as Level)
  • Charge Counter: Battery capacity in microampere-hours
  • Energy Counter: Battery remaining energy in nanowatt-hours
  • Current Average: Average battery current in microamperes
  • Current Now: Instantaneous battery current in microamperes
  • Is Charging: Whether battery is currently considered to be charging
  • Charge Time Remaining: Approximation for how much time (in milliseconds) remains until the battery is fully charged.

Step by Step Explanation

If you want to make this app from scratch, follow these steps:

1. Open Android Studio. Create new project with “Empty Activity” option.

2. Change the minimumSdkVersion to 21 in the app/build.gradle file. This is required for obtaining BatteryManager service.

3. Lets’s make the layouts first. We want to create list of battery status. So, we need to add a RecyclerView to the activity_main.xml file and also create a new layout for the item called item_battery_data.xml. Here’s what they look like and their codes:

The activity_main.xml should look like this.
The item_battery_data.xml should look like this.

4. Now, we want to create a model for the battery status named BatteryData.kt. This model is just a simple data class containing all the information displayed in item_battery_data.xml. Here’s what the model looks like:

5. Next, we want to create a RecyclerView adapter class for displaying list of battery status. Let’s name this file BatteryDataAdapter.kt. Here’s what the adapter looks like:

6. Next, let’s create a BroadcastReceiver to receive an action called ACTION_BATTERY_CHANGED. This action is broadcasted by the system every time there is a change in battery condition. Let’s name this BroadcastReceiver BatteryManagerBroadcastReceiver.kt. Here’s what it looks like:

7. Now, let’s create a helper class for displaying some battery properties named BatteryUtil.kt. BatteryManager API returns some properties that need to be interpreted first before it could be readable for human. Here’s what it looks like:

8. Finally, let’s update MainActivity.kt. In summary, what we do here is register (and unregister) the BatteryManagerBroadcastReceiver and display the list of battery status via BatteryDataAdapter:

9. That’s it! You can run the app in your device and it should look like this:

The final result of the application.

Thanks for reading!