Android - Getting Device Battery Information

    Sometimes, by developing some applications which related to device hardware, you must get some device information. In the previous post, I had presented the way to get some basic device information like device name, OS level, Wifi status,... Today, I  dedicated one separated post to talk about getting battery details in Android through using BatteryManager to help you have  a deeper understanding of this specific API.
    The battery API is quite impressive, between other things not only can you get a battery’s voltage but you can also get the temperature, status and even an icon representing the current state of the battery...

Initializing and registering a BroadcastReceiver

    Firstly, you must create a BroadcastReceiver variable and register it with an IntentFilter named ACTION_BATTERY_CHANGED. Your onStart() and onStop() of your main activity will be like this:
    protected void onStart() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver, filter);

    protected void onStop() {

    private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            //More code will be adding later

Overriding onReceive() of the BroadcastReceiver

    Now, we need this BroadcastReceiver receiving data with Battery Status change, so override onReiceive() and get information from Intent:
private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            boolean isPresent = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
            String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
            int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
            int level = 0;

            //If a battery is present
            if (isPresent) {
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                batteryFeatures.add(new BatteryFeature("Battery level", String.valueOf(level)));
                batteryFeatures.add(new BatteryFeature("Technology", technology));
                batteryFeatures.add(new BatteryFeature("Plugged", getPlugTypeString(plugged)));
                batteryFeatures.add(new BatteryFeature("Health", getHealthString(health)));
                batteryFeatures.add(new BatteryFeature("Status", getStatusString(status)));
                batteryFeatures.add(new BatteryFeature("Voltage", String.valueOf(voltage)));
                batteryFeatures.add(new BatteryFeature("Temperature", String.valueOf(temperature)));

            } else {
                //This case can be happened when you use a virtual device like Genymotion mobile
                textView.setText("Battery not present!!!");

More methods to get some important features

    You need 3 following methods to get Battery plug type, health type and it's status:
  • getPlugTypeString(): Detecting charging type: through AC or USB
  • getHealthString(): get Battery healthy type
  • getStatusString(): return battery status
    And this is 3 methods code in your main activity:
private String getPlugTypeString(int plugged) {
        String plugType = "No Plugged";

        switch (plugged) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                plugType = "AC";
            case BatteryManager.BATTERY_PLUGGED_USB:
                plugType = "USB";

        return plugType;

    private String getHealthString(int health) {
        String healthString = "Unknown";

        switch (health) {
            case BatteryManager.BATTERY_HEALTH_DEAD:
                healthString = "Dead";
            case BatteryManager.BATTERY_HEALTH_GOOD:
                healthString = "Good";
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                healthString = "Over Voltage";
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                healthString = "Over Heat";
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                healthString = "Failure";

        return healthString;

    private String getStatusString(int status) {
        String statusString = "Unknown";

        switch (status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                statusString = "Charging";
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                statusString = "Discharging";
            case BatteryManager.BATTERY_STATUS_FULL:
                statusString = "Full";
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                statusString = "Not Charging";

        return statusString;

Running application

    In this sample project, I'll show battery information to a RecyclerView, so when running this app, you'll have this output (for full source code please go to end of this post):
    When you are charging device through USB port:
    When you are not charging:


    With some simple code with using BroadcastReceiver and some properties of BatteryManager class, you can easy to get the device battery information. I hope that my sample project can be helpful with your work. For full code, please click at the button below! Thanks for reading!


Previous post
« Prev Post
Next post
Next Post »