I want to know if the charger attached to phone is charging the phone or not. Use case is, if you plug in charger but forget to switch on the charging, you should get a notification.
Asked
Active
Viewed 714 times
1
-
Short answer, no. Why would you need this, your phone should notify you when it is charging anyway, if you don't get the notification, then it isn't charging... – GTBebbo Mar 07 '20 at 14:59
-
Please read the description before down voting a question. I clearly said I want a notification if the charger is plugged in but user forgets to switch on the power button. – Rana Mar 07 '20 at 15:04
-
Does this answer your question? [Get battery level and state in Android](https://stackoverflow.com/questions/3291655/get-battery-level-and-state-in-android) – lenik Mar 07 '20 at 15:11
-
@lenik, I don't think so. Battery level and status I can get but I need that with the combination of USB plugged in or not. – Rana Mar 07 '20 at 15:17
-
My logic is very simple, if( usbPluggedIn && !charging)notify(); . For this I just need a reliable meathod to distinguish between 'usb plugged in and not charging' vs 'no usb plugged in' – Rana Mar 07 '20 at 15:18
1 Answers
0
The BatteryManager broadcasts all battery and charging details in a sticky Intent that includes the charging status. Check for BatteryManager.BATTERY_STATUS_FULL
.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
More details are available at: https://developer.android.com/training/monitoring-device-state/battery-monitoring

lenik
- 23,228
- 4
- 34
- 43
-
Hi, thanks for the response. Will this give different results if the usb is plugged in and not charging vs no usb connection? – Rana Mar 07 '20 at 15:06
-
@Rana you may need to try to find out, I've never checked that particular situation. – lenik Mar 07 '20 at 15:09