Get battery level only once using Android SDK [duplicate]
Asked Answered
G

1

33

I've searched on the web and couldn't find the answer to my question. My problem is to get the battery level information only once, eg. calling the function getBatteryLevel(). There are only solutions which are implemented using BroadcastReceiver, but as I know it will be called every time on battery level's change event. Please, tell me how can I get that information only once?

Gallice answered 1/4, 2013 at 15:50 Comment(1)
Use a BroadcastReceiver and then once you get the value once just unregiester the receiver.Puke
K
100

The Intent.ACTION_BATTERY_CHANGED broadcast is what's known as a "sticky broadcast." Because this is sticky, you can register for the broadcast with a null receiver which will only get the battery level one time when you call registerReceiver.

A function to get the battery level without receiving updates would look something like this:

public float getBatteryLevel() {
    Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed but I added just in case.
    if(level == -1 || scale == -1) {
        return 50.0f;
    }

    return ((float)level / (float)scale) * 100.0f; 
}

More data can be pulled from this sticky broadcast. Using the returned batteryIntent you can access other extras as outlined in the BatteryManager class.

Krebs answered 1/4, 2013 at 16:4 Comment(8)
This method cannot now be called from withing a Receiver (e.g. a Widget). Do you know of any other way to get the battery level that can be done from within a Widget's onUpdate() method?Nates
If you're doing this from a widget, you'll need to call registerReceiver like so: getApplicationContext().registerReciever(...) for it to work.Krebs
I have a context and can call it, but the call is not permitted from within a receiver, passing this or null as the first parameter "receiver". Log message is very explicit that you cannot call from within a receiver.Nates
You can't use just any context, you need to use the context returned from getApplicationContext() to call registerReceiver() from within another receiver. I have tried this and it works. It is also the answer given in this question that is very similar to the one you're asking: #3692079Krebs
With ApplicationContext that worked perfectly! Thanks!Nates
Any special permission required for achieving this?Andres
No permissions required according to the Android documentation.Krebs
This is the only way for a one-time power status query on startup. Using an arbitrary new intent won't work in that case. But subsequent power hotplug/unplug can be detected by a broadcast event.Luciferin

© 2022 - 2024 — McMap. All rights reserved.