How to detect that Android device doesn't have built-in battery?

184 Views Asked by At

Some of my users have head unit devices on Android (in their cars) and of course such device doesn't have battery and the next code returns 0

val batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)

and the next method will return false

val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
val statusCharging = status == BatteryManager.BATTERY_STATUS_CHARGING

I have a video recording app and it stops recording automatically if batteryLevel <= 1 && !statusCharging

And it stops recording for such devices which don't have any battery

1

There are 1 best solutions below

2
Henry Twist On BEST ANSWER

You can use BatteryManager.EXTRA_PRESENT which according to the documentation, exposes a:

boolean indicating whether a battery is present.