Jan 10, 2021
Get battery level and state in android programmatically
Just add this function to the any object class or any activity and call it. and function will return you the battery level.
Remove @JvmStatic if call from Kotlin.
add @JvmStatic if calling from java.
@JvmStatic
fun getBatteryPercent(context : Context): Int {val bm = context.getSystemService(BATTERY_SERVICE)
as BatteryManager
return bm.getIntProperty
(BatteryManager.BATTERY_PROPERTY_CAPACITY);
}