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);
}
Sanjay S Gangwar
Sanjay S Gangwar

Written by Sanjay S Gangwar

0 Followers

Just an average android developer.

No responses yet