简体   繁体   中英

How to detect if charger attached to phone is not charging phone?

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.

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

Phone battery getting stuck is a pretty common problem. I was facing the same issue. But luckily, there are some ways that can solve it. I tried numerous things. Here, this guide can prove to be really helpful.

https://techtista.com/2022/07/09/my-phone-wont-charge-past-1-percent-reasons-and-fixes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM