How to find out if the phone is charging

I need to know (through my application) if the Android device is charging. Any ideas? thanks

+3
source share
3 answers

One thing I found out about is that if your phone has a 100 percent battery level, you won’t receive a payment notification. Some people mean charging, while others mean when it has external power, regardless of whether it is 100% or not. I focused them on one thing, and if any condition is true, I will return.

public static boolean isPhonePluggedIn(Context context){ boolean charging = false; final Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean batteryCharge = status==BatteryManager.BATTERY_STATUS_CHARGING; int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; if (batteryCharge) charging=true; if (usbCharge) charging=true; if (acCharge) charging=true; return charging; } 
+8
source

Here is the code that worked:

 Intent bat = loader.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int level = bat.getIntExtra("level", 0); int scale = bat.getIntExtra("scale", 100); return level * 100 / scale; 
+5
source
  public static boolean isBatteryCharging(Context context){ // Check battery sticky broadcast final Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); return (batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) == BatteryManager.BATTERY_STATUS_CHARGING); } 
0
source

All Articles