Device battery info

Updated in Android

All Android devices that I have encountered have contained a battery so I’m pretty sure that it’s safe to say that almost all of them have one. Since batteries are the most important power source for the Android devices it’s no surprise that the operating system provides detailed information about them.

In this article we’re taking a look at how to get that information, parse it and show it to the user.

Basics

The battery information is accessed through the ACTION_BATTERY_CHANGED intent. The important thing to note is that the intent is a sticky one, i.e. there’s always one available so that you don’t need to wait for the event to happen.

After getting the intent we can parse the information with the help of BatteryManager class. Example below shows this in detail.

Example

package com.tanelikorri.android.batteryinfo;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class BatteryActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_battery);

        // Get the sticky battery changed intent
        IntentFilter intentFilter = new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED);
        Intent statusIntent = registerReceiver(null, intentFilter);

        // Battery charger type
        int chargePlug = statusIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
                -1);
        String chargerType = "-";
        switch (chargePlug) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                chargerType = "AC";
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                chargerType = "USB";
                break;
            case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                chargerType = "Wireless";
                break;
        }
        ((TextView) findViewById(R.id.chargerType)).setText(chargerType);

        // Battery status
        int statusType = statusIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
                BatteryManager.BATTERY_STATUS_UNKNOWN);

        String batteryStatus = null;
        switch (statusType) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                batteryStatus = "Charging";
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                batteryStatus = "Discharging";
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                batteryStatus = "Full";
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                batteryStatus = "Not charging";
                break;
            case BatteryManager.BATTERY_STATUS_UNKNOWN:
                batteryStatus = "Unknown";
                break;
        }
        ((TextView) findViewById(R.id.batteryStatus)).setText(batteryStatus);

        // Battery level
        int level = statusIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = statusIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        String batteryLevel = Integer
                .toString((int) (((float) level / (float) scale) * 100));
        ((TextView) findViewById(R.id.batteryLevel)).setText(batteryLevel);

        // Battery health
        int healthType = statusIntent.getIntExtra(BatteryManager.EXTRA_HEALTH,
                BatteryManager.BATTERY_HEALTH_UNKNOWN);
        String batteryHealth = null;
        switch (healthType) {
            case BatteryManager.BATTERY_HEALTH_COLD:
                batteryHealth = "Cold";
                break;
            case BatteryManager.BATTERY_HEALTH_DEAD:
                batteryHealth = "Dead";
                break;
            case BatteryManager.BATTERY_HEALTH_GOOD:
                batteryHealth = "Good";
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                batteryHealth = "Over voltage";
                break;
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                batteryHealth = "Overheat";
                break;
            case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                batteryHealth = "Unknown";
                break;
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                batteryHealth = "Unspecified failure";
                break;
        }
        ((TextView) findViewById(R.id.batteryHealth)).setText(batteryHealth);

        // Battery technology
        String batteryTech = statusIntent
                .getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
        ((TextView) findViewById(R.id.batteryTech)).setText(batteryTech);

        // Battery temperature
        String batteryTemp = null;
        int temperatureInt = statusIntent.getIntExtra(
                BatteryManager.EXTRA_TEMPERATURE, Integer.MIN_VALUE);
        if (temperatureInt != Integer.MIN_VALUE) {
            batteryTemp = Float.toString(((float) temperatureInt) / 10) + " °C";
        }
        ((TextView) findViewById(R.id.batteryTemp)).setText(batteryTemp);

        // Battery voltage
        String batteryVoltage = null;
        int voltageInt = statusIntent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,
                Integer.MIN_VALUE);
        if (voltageInt != Integer.MIN_VALUE) {
            batteryVoltage = Float.toString(((float) voltageInt) / 1000) + " V";
        }
        ((TextView) findViewById(R.id.batteryVoltage)).setText(batteryVoltage);
    }

}

In the above example activity the battery information is fetched from the intent one by one. Most of the information processing is pretty normal, you get the value and compare it to the possible states that have been set in the BatteryManager class. But there are some special cases, like battery level, temperature and voltage.

When calculating the battery level, you need to take the given scale into account or else you’ll might get some weird numbers. With temperature you’ll need to divide the returned value to get the temperature in celsius degrees. And the voltage is returned as millivolts so dividing it by 1000 will give you the voltage in volts.

Screenshots

Battery info

Source code

Source code for the whole example project is available here

Further reading