I'm using the BATTERY_SERVICE on Android studio and I want to get the changes on the battery levels using this code:
val bm = applicationContext.getSystemService(BATTERY_SERVICE) as BatteryManager
// Get battery change
val level: Int = bm.getIntProperty(BatteryManager.EXTRA_LEVEL,-1)
val scale: Int = bm.getIntProperty(BatteryManager.EXTRA_SCALE,-1)
The last two lines show this error:
Type mismatch: inferred type is String but Int was expected
How can solve it?
I don't find a different method from getIntProperty to extract those values. For example, the method getIntExtra is not available, and I think EXTRA_LEVEL it would fail too. Is there any way to change the Straing values to Int so they are accepted by the method or is there any other better approach?
Thanks for your help, you guided me to solve it using this:
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
getIntProperty()is used to read properties, not extras. You would usegetIntProperty()with things likeBATTERY_PROPERTY_ENERGY_COUNTERand other values identified byBATTERY_PROPERTY_...constants, notEXTRA_...constants.