Get current battery level once on Android platform by using Battery_Changed Intent. This gets total scale value and current level to calculate.
public float getCurrentBatteryLevel() {
Intent battery = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int batteryLevel = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int batteryScale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
//to avoid error use this
if(batteryLevel == -1 || batteryScale == -1) {
return 50.0f;
}
return ((float)batteryLevel / (float)batteryScale) * 100.0f;
}
public float getCurrentBatteryLevel() {
Intent battery = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int batteryLevel = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int batteryScale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
//to avoid error use this
if(batteryLevel == -1 || batteryScale == -1) {
return 50.0f;
}
return ((float)batteryLevel / (float)batteryScale) * 100.0f;
}