мой код
Код: Выделить всё
lifecycleScope.launch(Dispatchers.IO) {
while (true) {
currentNow = try {
batteryManager?.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW) ?: 1
} catch (_: DeadSystemException) {
1
}
val curValue = limitDigits(currentNow)
val cur = "$curValue mA"
val cur1 = "$batteryCur : $cur"
val power = (curValue.toInt() * batteryVoltage) / 1000000.0
val powerDis = "%.2f".format(power) + " W"
val powerDisTop = "$batteryPow : $powerDis"
withContext(Dispatchers.Main) {
txtBatteryCurrentDis.text = cur
txtBatteryCurrentTop.text = cur1
txtBatteryPowerDis.text = powerDis
txtBatteryPowerTop.text = powerDisTop
}
delay(1000)
}
}
private fun limitDigits(n: Int): String {
val abs = abs(n)
var tempValue = abs.toString()
var absoluteValueFinal = abs
try {
absoluteValueFinal = if (isCharging) absoluteValueFinal else absoluteValueFinal * -1
if (tempValue.length >= 6) {
absoluteValueFinal /= 1000
tempValue = abs(absoluteValueFinal).toString()
if (tempValue.length >= 6) {
absoluteValueFinal /= 1000
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}
return if (n != 0 && n != Int.MIN_VALUE && n != Int.MAX_VALUE) {
if (isBatteryFull) {
return "0"
} else {
absoluteValueFinal.toString()
}
} else {
"0"
}
}
Код: Выделить всё
Caused by: android.os.DeadSystemException:
android.os.BatteryManager.queryProperty(BatteryManager.java:274)
android.os.BatteryManager.getIntProperty(BatteryManager.java:290)
com.ytheekshana.deviceinfo.fragments.TabBattery$onCreateView$2.invokeSuspend(TabBattery.kt:226)
kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101)
kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.java:113)
kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:589)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:823)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:720)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:707)
Подробнее здесь: https://stackoverflow.com/questions/792 ... ry-manager