battery status on ACTION_POWER_CONNECTED

坚强是说给别人听的谎言 提交于 2020-01-04 14:29:13

问题


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:


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.



来源:https://stackoverflow.com/questions/8024082/battery-status-on-action-power-connected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!