Search code examples
androidbatterylevel

Which is the difference between two methods to calculate the battery level in android?


in my application i display the battery level.. i get the battery level in this way:

int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

And it's ok i can see the percentage.. but online i found another way:

int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale;

What's the difference? Why i would divide the level with the scale? Thanks


Solution

  • Well, you get different numbers :)

    the scale is the integer containing the maximum battery level. So with the second method you get the percentage (hence it is called batteryPct) you have left. With the first you get the asolute number of your battery level. It is defined as integer field containing the current battery level, from 0 to EXTRA_SCALE.