Home‎ > ‎RF Signal Tracker‎ > ‎

    How To: Monitor Battery Level

    Ok, you have created an app that may use a lot juice -- because it runs in the background, keeps the screen on, whatever -- and you want a way to monitor the battery level and maybe turn the app off if the battery gets too low. Here is a fast and dirty how to.

    Step 1: Build a BroadcastReceiver

    The receiver overrides the onReceive() method. You test to see if the particular intent action being broadcast is a battery level change broadcast (there are many others). In the code below that intent is placed in the string "action".

    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    String action = intent.getAction(); 

            }
    }

    Step 2: Read the intent

    Check to see if a battery change was broadcast with a simple if statement.

    if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {

    }

    If this was a battery change then get the current battery level, the scale that level is measured against, and whether or not the device is plugged in. This is done by pulling out the different parts of the broadcast intent.

    int level = intent.getIntExtra("level", 0);
    int scale = intent.getIntExtra("scale", 100);
    int plugged = intent.getIntExtra("plugged", 0);

    I like to see if the device is plugged in first since, if it is, there is nothing really that needs to be done. If the device is running off of its battery, then plugged will be zero.

    if(plugged == 0){

    }

    Finally you check the battery level and act accordingly.

    if((level * 100 / scale) < MIN_BATTERY_LEVEL){
        // do something...
    }

    Where MIN_BATTERY_LEVEL is, of course, the minimum battery level you want to act on. In this example, if MIN_BATTERY_LEVEL=20 then the code would execute when the battery is below 20%. The whole BroadcastReceiver method is:

    private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction(); 
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                int level = intent.getIntExtra("level", 0);
                int scale = intent.getIntExtra("scale", 100);
                int plugged = intent.getIntExtra("plugged", 0);

                if(plugged == 0){
                    if((level * 100 / scale) < MIN_BATTERY_LEVEL){
                        // do something...
                    }
                }
            }
        }
    };

    Step 3: Register/Unregister your listeners

    In your onResume() method register the listener like so:

    @Override 
    protected void onResume() {
        super.onResume(); 
        registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
    }

    And, likewise, unregister your listener in your onPause() so it gets called when you leave the app.

    @Override 
    protected void onPause() {
        super.onPause(); 
        unregisterReceiver(mBatteryInfoReceiver);
    }

    That's it! For more info on broadcast receivers look here.
    Comments