I'm trying to detect when a user plugs (or unplugs) in their device to charge. In my receiver, where I determine whether it is plugged in or not, I always get a "false" reading on the status. Here is my code:
(In the manifest):
<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>
Here is the PowerConnectionReceiver class:
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;
Log.d("Battery", "Plugged In: " + String.valueOf(isCharging));
Log.d("Battery", "status: " + String.valueOf(status));
}
}
What's working: The PowerConnectionReceiver properly gets called when the phone is plugged in.
However, when I print out the status, it always returns as -1 (which is the default value I entered in). It seems the BatteryManager.EXTRA_STATUS isn't coming in properly.
For reference, here is what those Logs are printing out:
"Plugged In: false"
"status: -1"
More reference - here is the page on the Developers site I am using for this: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html