Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED
Asked Answered
S

6

42

I have a broadcast receiver in my program to get react to the battery level like so:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {
        int level = intent.getIntExtra("level", 0);
        // do something...
    }
}

    registerReceiver(this.mBatInfoReceiver, 
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

However this code has to wait for the battery status to be updated so if you have a GUI element that needs to be set based on the battery level it must wait for a battery event to occur. Is there a way to nudge this to get it working or simply run some code to see what the battery level was on the last broadcast?

Sarah answered 7/9, 2010 at 18:21 Comment(0)
W
38

Is there a way to nudge this to get it working or simply run some code to see what the battery level was on the last broadcast?

You can call registerReceiver() with your IntentFilter and a null BroadcastReceiver to get the last-broadcast Intent. This works because ACTION_BATTERY_CHANGED is a so-called "sticky broadcast", which I describe a bit more in this StackOverflow question-and-answer.

Worsham answered 7/9, 2010 at 19:19 Comment(3)
The receiver I have in the question actually is called as soon as it is created I just didn't notice because I reinitialised the variables after it.Sarah
Will getting the applicationContext as described below to make this call cause problems? I have the problem that my broadcast receiver isn't allowed to register for intents, even though I pass in null. What is the correct solution to this problem?Language
@Mayra: getApplicationContext() should be fine here, particularly if you are trying to do this from a BroadcastReceiver.Worsham
C
84

This is how to get the battery level without registering a receiver:

Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
                    new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
double level = -1;
if (rawlevel >= 0 && scale > 0) {
    level = rawlevel / scale;
}

It can use a null BroadcastReceiver because of the sticky nature of the broadcast.

It uses the getApplicationContext() trick in case you are in a intent receiver and get the exception:

android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents

Cellulosic answered 28/10, 2010 at 22:57 Comment(4)
Under certain situations (like running in a system service), getApplicationContext() will return null. In that case the call should be: context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));Seel
@Seel wrote "Under certain situations (like running in a system service), getApplicationContext() will return null". I don't think that's always true. Perhaps it depends on the type of service.Aldershot
Perfect! I can do it without service now.Infralapsarian
I'm pretty sure that this is the only way to get access the battery info from within a BroadcastReceiver without getting a ReceiverCallNotAllowedException. Thanks for a useful tip! :)Shondrashone
W
38

Is there a way to nudge this to get it working or simply run some code to see what the battery level was on the last broadcast?

You can call registerReceiver() with your IntentFilter and a null BroadcastReceiver to get the last-broadcast Intent. This works because ACTION_BATTERY_CHANGED is a so-called "sticky broadcast", which I describe a bit more in this StackOverflow question-and-answer.

Worsham answered 7/9, 2010 at 19:19 Comment(3)
The receiver I have in the question actually is called as soon as it is created I just didn't notice because I reinitialised the variables after it.Sarah
Will getting the applicationContext as described below to make this call cause problems? I have the problem that my broadcast receiver isn't allowed to register for intents, even though I pass in null. What is the correct solution to this problem?Language
@Mayra: getApplicationContext() should be fine here, particularly if you are trying to do this from a BroadcastReceiver.Worsham
C
6
    public static String batteryLevel(Context context)
    {
        Intent intent  = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));   
        int    level   = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int    scale   = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        int    percent = (level*100)/scale;
        return String.valueOf(percent) + "%";
    }
Charmine answered 12/5, 2014 at 16:31 Comment(0)
E
2
// Put this Code into your MainActivity

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent i) {
        int level = i.getIntExtra("level", 0);
        ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
        pb.setProgress(level);
        TextView tv = (TextView) findViewById(R.id.textfield);
        tv.setText("Battery Level: " + Integer.toString(level) + "%");
    }

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    registerReceiver(mBatInfoReceiver, new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED));
}
Effloresce answered 23/4, 2013 at 10:14 Comment(1)
may i need to unresgiter this reciever as well in onPause i m unregistering but this is creating error 01-11 15:08:21.711: E/AndroidRuntime(31730): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.example.batterylevel.ChargeActivity$1@426304a0Communication
C
1

I use this method to get the battery level without receiving updates.

public float getMyBatteryLevel() {
        Intent batteryIntent = this.getApplicationContext().registerReceiver(null,
        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        return batteryIntent.getIntExtra("level", -1);
}
Companionship answered 4/2, 2014 at 19:9 Comment(0)
U
0

Please be aware, that the intent you get from registerReceiver call

Intent intent  = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

can be null. So please make a check before using the intent, e.g.

if(intent != null){ 
// do your stuff here... 
}

I just got a null pointer exception, causing the app to crash!

Unsheathe answered 24/8, 2016 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.