Harmony Valley
Harmony Valley

Reputation: 145

how to solve inferred type is String but Int was expected in Kotlin method?

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)

Upvotes: 0

Views: 88

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007494

getIntProperty() is used to read properties, not extras. You would use getIntProperty() with things like BATTERY_PROPERTY_ENERGY_COUNTER and other values identified by BATTERY_PROPERTY_... constants, not EXTRA_... constants.

Upvotes: 1

Related Questions