标签:extra span ext cas manage eal status 电源 监控
定义接受电源信息的BroadCastReceiver
private val batteryBroadcastReceiver = object :BroadcastReceiver(){ override fun onReceive(p0: Context?, p1: Intent?) { when(p1?.getIntExtra(BatteryManager.EXTRA_STATUS,BatteryManager.BATTERY_HEALTH_UNKNOWN)){ BatteryManager.BATTERY_STATUS_CHARGING -> { //充电中 } BatteryManager.BATTERY_STATUS_FULL -> { //已充满 } else -> { val level = p1?.getIntExtra(BatteryManager.EXTRA_LEVEL,0)//当前电量 val scale = p1?.getIntExtra(BatteryManager.EXTRA_SCALE,0)//总电量 val percentage = (level?.toFloat()!! * 100 / scale?.toFloat()!!).toInt() } } } }
注册和销毁
context.registerReceiver(batteryBroadcastReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
context.unregisterReceiver(batteryBroadcastReceiver)
标签:extra span ext cas manage eal status 电源 监控
原文地址:https://www.cnblogs.com/rchao/p/11231486.html