Start by determining the current state of charge. BatteryManager transmits all battery and charging data in a sticky manner, which includes charging status.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter);
You can extract the current state of charge in this way
you can use this
public static final String EXTRA_LEVEL
Added in API level 5 Extra for ACTION_BATTERY_CHANGED: integer field containing the current battery level, from 0 to EXTRA_SCALE. Constant Value: "level"
You can find the current battery charge by extracting the current battery level and scale from the battery status, as shown below:
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale;
so batteryPct is your battery% Percent
// you can show your percentage and then
More about BatteryManager from here
source share