This is how to get the battery level without registering a receiver:
Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
double level = -1;
if (rawlevel >= 0 && scale > 0) {
level = rawlevel / scale;
}
It can use a null BroadcastReceiver because of the sticky nature of the broadcast.
It uses the getApplicationContext() trick in case you are in a intent receiver and get the exception:
android.content.ReceiverCallNotAllowedException:
IntentReceiver components are not
allowed to register to receive
intents