Broadcast receiver - monitoring the Battery Level and Charging State

前端 未结 7 2182
逝去的感伤
逝去的感伤 2020-12-31 23:40

I am trying to make two toasts: one when the device is charging, and one when it`s not. But the receiver acting crazy, sending many toasts, and crashing the app. I can not f

7条回答
  •  情书的邮戳
    2021-01-01 00:01

    BroadcastReceiver receives charging state. That's why continuously triggering toast message.

     boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                    status == BatteryManager.BATTERY_STATUS_FULL
    

    instead of

      boolean isCharging =status= BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    

    please refer this link: Check if device is plugged in

提交回复
热议问题