How to get Battery Information programmatically in Android ?
Android OS lets to developers to get a lot of data about features of a device. Amongst these data, it’s possible to get Battery Information for exemple. An application like CPU Hardware and System Info is able to give you following information about your Battery :
- Present or No
- Health
- Level
- Plugged
- Charging Status
- Technology
- Temperature
- Voltage
- Capacity
You can see all these information on Battery tab of CPU Hardware and System Info :
So, how CPU Hardware and System Info makes to give you these information ? Application use Android Battery API. Data can be got by registering a BroadcastReceiver on an Activity with correct filters :
private void loadBatterySection() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_POWER_CONNECTED); intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryInfoReceiver, intentFilter); }
Here, we choose to receive info when data about battery changes with intent ACTION_BATTERY_CHANGED but also to be alerted when Battery is plugged or no. Here, batteryInfoReceiver is an instance of BroadcastReceiver :
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateBatteryData(intent); } };
When onReceive method is called, the instance of Intent in parameter contains data about the Battery. We create a method updateBatteryData with following content :
private void updateBatteryData(Intent intent) { boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); if (present) { int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0); int healthLbl = -1; switch (health) { case BatteryManager.BATTERY_HEALTH_COLD: healthLbl = R.string.battery_health_cold; break; case BatteryManager.BATTERY_HEALTH_DEAD: healthLbl = R.string.battery_health_dead; break; case BatteryManager.BATTERY_HEALTH_GOOD: healthLbl = R.string.battery_health_good; break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: healthLbl = R.string.battery_health_over_voltage; break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: healthLbl = R.string.battery_health_overheat; break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: healthLbl = R.string.battery_health_unspecified_failure; break; case BatteryManager.BATTERY_HEALTH_UNKNOWN: default: break; } if (healthLbl != -1) { // display battery health ... healthTv.setText("Health : " + getString(healthLbl)); } // Calculate Battery Pourcentage ... int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); if (level != -1 && scale != -1) { int batteryPct = (int) ((level / (float) scale) * 100f); batteryPctTv.setText("Battery Pct : " + batteryPct + " %")); } int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); int pluggedLbl = R.string.battery_plugged_none; switch (plugged) { case BatteryManager.BATTERY_PLUGGED_WIRELESS: pluggedLbl = R.string.battery_plugged_wireless; break; case BatteryManager.BATTERY_PLUGGED_USB: pluggedLbl = R.string.battery_plugged_usb; break; case BatteryManager.BATTERY_PLUGGED_AC: pluggedLbl = R.string.battery_plugged_ac; break; default: pluggedLbl = R.string.battery_plugged_none; break; } // display plugged status ... pluggedTv.setText("Plugged : " + getString(pluggedLbl)); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); int statusLbl = R.string.battery_status_discharging; switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: statusLbl = R.string.battery_status_charging; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: statusLbl = R.string.battery_status_discharging; break; case BatteryManager.BATTERY_STATUS_FULL: statusLbl = R.string.battery_status_full; break; case BatteryManager.BATTERY_STATUS_UNKNOWN: statusLbl = -1; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: default: statusLbl = R.string.battery_status_discharging; break; } if (statusLbl != -1) { chargingStatusTv.setText("Battery Charging Status : " + getString(statusLbl)); } if (intent.getExtras() != null) { String technology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY); if (!"".equals(technology)) { technologyTv.setText("Technology : " + technology); } } int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0); if (temperature > 0) { float temp = ((float) temperature) / 10f; tempTv.setText("Temperature : " + temp + "°C); } int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0); if (voltage > 0) { voltageTv.setText("Voltage : " + voltage + " mV); } long capacity = getBatteryCapacity(this); if (capacity > 0) { capacityTv.setText("Capacity : " + capacity + " mAh"); } } else { Toast.makeText(this, "No Battery present", Toast.LENGTH_SHORT).show(); } }
In the previous code, you can notice the getBatteryCapacity method that will work only Android versions superior to Lollipop. The code is the following :
public long getBatteryCapacity(Context ctx) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { BatteryManager mBatteryManager = (BatteryManager) ctx.getSystemService(Context.BATTERY_SERVICE); Long chargeCounter = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER); Long capacity = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); if (chargeCounter != null && capacity != null) { long value = (long) (((float) chargeCounter / (float) capacity) * 100f); return value; } } return 0; }
Leave a Reply
You must be logged in to post a comment.