1

While following the Android documentation for monitoring changes in battery status, I came across this:

<receiver android:name=".PowerConnectionReceiver">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  </intent-filter>
</receiver>

Supposedly, this allows you to register a BroadcastReceiver through the manifest for changes in the battery status. In the receiver you could do this.

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                            status == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    }
}

But though I can get the battery level when this Broadcast is sent, the Batterymanger.EXTRA_STATUS extra always seems to be null. Is this a mistake in the docs or am I doing something wrong?

Ventis
  • 488
  • 6
  • 22
  • 1
    According to [this portion of the documentation](https://developer.android.com/reference/android/os/BatteryManager.html), those extras are for `ACTION_BATTERY_CHANGED`, and you are not listening for that broadcast. So, there is a documentation bug in one of the two places. – CommonsWare Feb 14 '17 at 14:43

0 Answers0