Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED

后端 未结 6 1792
囚心锁ツ
囚心锁ツ 2020-11-28 03:56

I have a broadcast receiver in my program to get react to the battery level like so:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
           


        
6条回答
  •  醉话见心
    2020-11-28 04:01

    Is there a way to nudge this to get it working or simply run some code to see what the battery level was on the last broadcast?

    You can call registerReceiver() with your IntentFilter and a null BroadcastReceiver to get the last-broadcast Intent. This works because ACTION_BATTERY_CHANGED is a so-called "sticky broadcast", which I describe a bit more in this StackOverflow question-and-answer.

提交回复
热议问题