battery status on ACTION_POWER_CONNECTED

2019-07-15 19:59发布

I'm just wondering if is there any possibility to obtain battery status in broadcast receiver class that fires on ACTION_POWER_CONNECTED? Documentation suggest not, but it is always worth to ask :)

Cheers Ray

1条回答
孤傲高冷的网名
2楼-- · 2019-07-15 20:36

Call registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)). The Intent that is returned is the last-broadcast ACTION_BATTERY_CHANGED broadcast, which has your battery status in its extras (see BatteryManager for the keys).

If you determine that you are calling it too soon, that ACTION_POWER_CONNECTED is invoked before ACTION_BATTERY_CHANGED gets updated, perhaps use AlarmManager to schedule yourself to wake up again in a few seconds, and check again then.

查看更多
登录 后发表回答