【问题标题】:when battery is low alert dialogbox show msg. When connect charger it show dialogbox and automatically dismiss dialogbox without any button当电池电量不足警报对话框显示消息。连接充电器时显示对话框并自动关闭对话框,无需任何按钮
【发布时间】:2022-01-09 13:27:22
【问题描述】:

这是MainActivity.kt文件中我的广播接收器的乐趣

private val mPlugInReceiver = object : BroadcastReceiver() {
    override 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()
dismissDialog()
               // openDialogForBatteryStatus(false)
            }
            Intent.ACTION_POWER_DISCONNECTED -> {
                if(batLevel > 20){
                    openDialogForBatteryStatus(false)
                }else
                {
                    Toast.makeText(context, "Power disconnected", Toast.LENGTH_SHORT).show()
                    openDialogForBatteryStatus(true)

                }          
            }
        }
    }
}

这是我的第一个BaseActivity 呼叫警报对话框:

open class BaseActivity : AppCompatActivity() {
val d
    fun openDialogForBatteryLevel(show: Boolean){
        val dialogView = layoutInflater.inflate(R.layout.dialog_battery_low, null)

        val dialog = AlertDialog.Builder(this@BaseActivity)
        dialog.setView(dialogView).setCancelable(false)

        val d= dialog.create()

        if(show){
            if(d.isShowing)
                d.show()
        }else{
            d.dismiss()
        }

        dialogView.txtViewDescription.text = "Your Phone Battery is low"

        dialogView.txtViewOk.setOnClickListener{
            this.finishAffinity()
        }

        d.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    fun openDialogForBatteryStatus(show: Boolean) {
        val dialogView = layoutInflater.inflate(R.layout.dialog_battery_low, null)

        val dialog = AlertDialog.Builder(this@BaseActivity)
        dialog.setView(dialogView).setCancelable(false)
        d= dialog.create()
        if(show){
            if(d.isShowing)
                d.show()
        }else{
            d.dismiss()
        }
        dialogView.txtViewDescription.text = "Please Remove charger"

        dialogView.txtViewOk.setOnClickListener{
            this.finishAffinity()

        }

        d.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

fun dismissDialog(){
if(d != null){
d.dismiss()
}
}
}

这是我的第二个 BaseActivity 更改第一个文件后调用警报对话框

fun openDialogForBatteryLevel(show: Boolean, message : String): AlertDialog{

    val dialog
    val dialogView

    var d

    if(alertD == null){
        dialog = AlertDialog.Builder(this@BaseActivity)
        dialogView = layoutInflater.inflate(R.layout.dialog_battery_low, null)
        dialog.setView(dialogView).setCancelable(false)
        d = dialog.create()
    }else{
        d = alertD
    }

    if(show){
        if(d.isShowing)
            d.show()
    }else{
        d.dismiss()
    }

    dialogView.txtViewDescription.text = message

    dialogView.txtViewOk.setOnClickListener{
        this.finishAffinity()
    }
    d.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

    return d
}

【问题讨论】:

  • 感谢 Philip Dukhov 编辑了我的代码,如有可能,请告诉我任何更改或更多解决方案。
  • 你想在这里完成什么?
  • 不到 20 个对话框出现时,我的插件对话框会自动关闭而无需单击按钮并开始充电。当再次充电超过 70 时对话框来移除充电器当我拔出对话框关闭时。当它比铃声播放 70 时,它不会停止,直到我从设备上拔下电缆。

标签: android kotlin kotlin-coroutines


【解决方案1】:

只需像这样更改您的代码

private val mPlugInReceiver = object : BroadcastReceiver() {
override 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){
       // Show dialog here
    }
    when (intent.action) {
        Intent.ACTION_POWER_CONNECTED -> {
            // Dismiss dialog
        }
    }
  }
}

【讨论】:

  • 感谢您的回复 Rajesh,但我已经在我的活动中完成了此操作,但它没有得到关闭对话框,请再次查看。
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多