Show the updated battery status in Android

Have a happy holidays. After a long time, I'm blog here such a good topic.

Here I explain about the battery status on real time. Yes, you can use BatteryManager to get the battery status. But you want to know the updated status on all time means, you need to use BroadcastReceiver.

You can add on your activity.

 private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
            int icon_small = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, 0);
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            boolean present = intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            String technology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
            int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
            int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);


            txt_battery.setText(
                    " Health : " + health + "\n" +
                            " Level : " + level + " % " + "\n" +
                            " Plugged : " + plugged + "\n" +
                            " Present : " + present + "\n" +
                            " Scale : " + scale + "\n" +
                            " Status : " + status + "\n" +
                            " Technology : " + technology + "\n" +
                            " Temperature : " + temperature + "\n" +
                            " Voltage : " + voltage + "\n");
            imageBatteryState.setImageResource(icon_small);
        }
    };


In OnCreate()
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

Comments

Popular posts from this blog

Clear the app data programmatically in android

convert an integer to a string in java

Print the large string values in android log cat.