简体   繁体   中英

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

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

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

boolean indicating whether a battery is present.

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