I am trying to display on an app if a phone is being charged by a plug, a usb cable or not charging at all. I am using the code below, it will display "charging via usb cable" if I am using a usb cable and it displays "not charging" if the phone battery is full or not charging. But when I am charging the phone via a plug then it will show "charging via usb cable". I am using a normal ac plug charger and not an ac plug charger where you can stick in a usb cable (if that matters). Why does this happen?
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, filter);
int chargeState = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
if(chargeState == BatteryManager.BATTERY_PLUGGED_USB)
{
chargingBy = "charging via usb cable";
}
else if (chargeState == BatteryManager.BATTERY_PLUGGED_AC)
{
chargingBy = "charging via plug";
}
else
{
chargingBy = "not charging";
}
There are three different types of USB ports:
The first two can be found on your computer and the third kind applies to “dumb” wall chargers
What phone model are you testing on?
EXTRA_STATUS
Is not used to know how the device is being charget
use EXTRA_PLUGGED
instead