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:
    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(batteryReceiver);
    }

    private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        @SuppressLint("SetTextI18n")
        @Override
        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() {
        @SuppressLint("SetTextI18n")
        @Override
        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.clear();
                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)));

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

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";
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                plugType = "USB";
                break;
        }

        return plugType;
    }

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

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

        return healthString;
    }

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

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

        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:

Conclusions

    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!
    References:

Share


Previous post
« Prev Post
Next post
Next Post »