BatteryChanged vs Polling Battery Level - State change Vs Instantly checking


Licensed User

After considerable research in the B4A help app and on here, I have yet to find a way to instantly poll the current battery level and I am wondering if it can be done.

Sub PhoneEvents_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent) is great and serves a very useful function if you need to capture the event of the battery level changing from one value to the next or if you need to know whether the phone was plugged into a charger to removed from a charger.

However, it is only partially useful if what you want to do is instantly poll the battery level and place the current level into a variable to make an instant conditional comparison (If BatteryLevel<LimitValue Then do something) without waiting for the level to change; which can take quite awhile in many cases.

There are many situations where you would want to instantly poll the battery level instead of capturing the event when the level state changes.

For example, preventing an application from doing something if the battery is below a certain level (and preventing it at the time your code if making the decision and not delay it until there is a state change with the battery), or, for example, preventing the app from loading if the battery is below a certain level (you would not actually prevent the app from loading, but you could cause it to quit nearly instantly after loading without doing much of anything) or even preventing the app from performing an intent such as loading a URL in an external web browser, etc. And there are an infinite number of things you could decide to do or not do if you could instantly poll the battery level.

The problem with capturing the event is you have no way of programmically knowing what the current level is right now (especially at the start of your app or within the first 1 to 5 minuets of your app starting) without waiting until the level state changes to trigger the PhoneEvents_BatteryChanged sub.

Anyhow, I do not know if this is a limitation of B4A, or rather it can be accomplished and I just do not know how to do it, or if this is a limitation of the Android OS (i.e. maybe it only sends out the battery level as a state change) or if this functioning was intentionally left out? Clarification would be greatly appreciated.

Thank you very much.


Licensed User
Erel, that was VERY helpful to mention the "sticky" attribute of this. Thanks.
Upvote 0