I have an app that displays if an android device is being charged by a plug, a usb cable, a wireless device or not charging at all. I am using the code below, it will display the correct charging method (if any) until I charge my phone (has android 4.1) or tablet (has android 4.3) into a usb charger in a car. It will say it's charging by a plug, when it should say a usb device. What is the reason behind this?
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, filter);
int chargeState = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if(chargeState == BatteryManager.BATTERY_PLUGGED_USB)
{
chargingBy = "charging via usb cable";
}
else if (chargeState == BatteryManager.BATTERY_PLUGGED_AC)
{
chargingBy = "charging via plug";
}
else if (chargeState == BatteryManager.BATTERY_PLUGGED_WIRELESS)
{
chargingBy = "charging via wireless device";
}
else
{
chargingBy = "not charging";
}