How to calculate the current battery level in Android

Hey, I'm trying to figure out the battery level at a specific time. I watched the tunings that implement the broadcast receiver and calculate the battery level onReceive (). But does this not mean that the level will be calculated if there is a broadcast? (For example, how is the change in battery speed in the battery)? I wanted to know how can I find the battery level at this particular moment?

+4
source share
1 answer

But does this not mean that the level will be calculated in the presence of translation?

Moreover, when the battery level changes, a broadcast is sent.

I wanted to know how to find the battery level at this particular moment?

From a technical point of view, you cannot.

What you can do is find out about the last broadcast of ACTION_BATTERY_CHANGED - IOW, the battery level the last time Android told everyone about the battery level:

 registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)) 

This will return the last Intent broadcast (since broadcasting with the battery will be sticky). BatteryManager.EXTRA_LEVEL and BatteryManager.EXTRA_SCALE are keys to additional features that contain information about the battery level.

+5
source

Source: https://habr.com/ru/post/1486296/


All Articles