I can't receive broadcast on battery state change?

前端 未结 3 1464
野趣味
野趣味 2020-12-06 17:07

I am having the exact same problem as this post: Battery broadcast receiver doesn't work. But it seems no one has answered that question.

Here is my BroadcastRec

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 17:34

    I just followed the Android Developer Guide's on Monitoring the Battery Level and Charging State and had immediate success. If BatteryLevelReceiver is it's own class then I would recommend:

    
        
           
           
        
    
    

    Addition

    I'm willing to guess that you wrote BatteryLevelReceiver as a nested class in ReceversAndServices. According to Receiver as inner class in Android, you cannot do that with non-static classes. You could make BatteryLevelReceiver a static class and register the receiver in onResume(), but then your app will need to be running to catch the events... Move your receiver to a separate class and register these Intents:

    
        
           
           
           
           
        
    
    

    (Not BATTERY_CHANGED as Darshan Computing pointed out.)

提交回复
热议问题