【发布时间】:2022-01-08 17:13:44
【问题描述】:
这是我在 mainactivity.kt 文件中的广播接收器乐趣
私有 val mPlugInReceiver = 对象:BroadcastReceiver() { 覆盖 fun onReceive(context: Context, intent: Intent) {
val bm = context.getSystemService(Application.BATTERY_SERVICE) as BatteryManager
val batLevel:Int = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
System.out.println("integerBatteryLevel ===="+batLevel)
if(batLevel < 20){
System.out.println("this is low battaroy below 20 % ===="+batLevel)
}else
{
System.out.println("this is nice battaroy more than 20 % ===="+batLevel)
}
when (intent.action) {
Intent.ACTION_POWER_CONNECTED -> {
Toast.makeText(context, "Power connected", Toast.LENGTH_SHORT).show()
openDialogForBatteryStatus(false)
}
Intent.ACTION_POWER_DISCONNECTED -> {
if(batLevel > 20){
openDialogForBatteryStatus(false)
}else
{
Toast.makeText(context, "Power disconnected", Toast.LENGTH_SHORT).show()
openDialogForBatteryStatus(true)
}
}
}
}
}
【问题讨论】:
标签: dialog alert device connect dismiss