You are getting -1
for both level
and scale
.
That is because you might be trying to broadcast ACTION_BATTERY_CHANGED
in the manifest.
ACTION_BATTERY_CHANGED
is a sticky intent and you cannot register a receiver to it in the manifest. Try the following
Intent i = new ContextWrapper(applicationContext).registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
// now you can get the level and scale from this intent variable
int level = i.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = i.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float battPct = level/(float)scale;
You would not neet to device a receiver for this intent, just use the above mentioned way, whereever you want to use it.