电源信息监控
Posted rchao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电源信息监控相关的知识,希望对你有一定的参考价值。
定义接受电源信息的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)
以上是关于电源信息监控的主要内容,如果未能解决你的问题,请参考以下文章