0

I was reading the guide here about setting up a BroadcastReceiver to check changes in the battery. And it stated that I could setup a BroadcastReceiver like this:

<receiver android:name=".PowerConnectionReceiver">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
    </intent-filter>
</receiver>

in my manifest and this as a class:

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                        status == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
    }
}

Is the BroadcastReceiver call onReceive(...) ONLY when a CHANGE occurs in the battery state? For example, what if the user had the device plugged into their computer and it was charging the entire time the BroadcastReceiver was running. Would it detect a change, since technically, the device's battery status didn't change? Whenever I get isCharging, would it be the current value, or the value at the last change? I want to check if it did change and what it always is so that I can optimize my application and save battery life.

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

3 Answers3

2

It will get called only on these actions, as you specified...

  1. android.intent.action.ACTION_POWER_CONNECTED
  2. android.intent.action.ACTION_POWER_DISCONNECTED

And that's when...

1) External power has been connected to the device.

2) External power has been removed from the device.

If you need to track battery power changes, you should include this filter in your manifest.

android.intent.action.ACTION_BATTERY_CHANGED

Btw. isCharging would be the current state, of course.

nullpotent
  • 9,162
  • 1
  • 31
  • 42
  • So if I were to create a method like `public boolean isCharging()` and it returns `isCharging`. `isCharging()` can be called at anytime, so would it accurately return if the device was charging? – Mohit Deshpande Jul 22 '12 at 15:24
  • I believe (almost certain :)) it would. Why not testing it to be sure? – nullpotent Jul 22 '12 at 15:25
  • `isCharging` would correctly indicate charging status only if `onReceive` gets called just before you call `isCharging()`. You need to put your `isCharging` evaluation code in `isCharging()` method. – nullpotent Jul 22 '12 at 15:44
  • P.S I would create a helper class and would name it cleverly just for specific static methods. I wouldn't couple it with `BroadcastReceiver`, for the sake of good design. – nullpotent Jul 22 '12 at 15:47
  • @AljoshBre thanks for the tip, but the evaluation code is dependent on the `Intent` in the `onReceive` – Mohit Deshpande Jul 22 '12 at 15:48
0

1. BroadcastReceiver uses the Publisher-Subscriber Pattern.

2. Its used in this way.." If xyz happens inform me, and i will take action on the basis of that".

  1. Its not only on the CHANGE in battery states, but many more, like When Booting is completed etc...

See this for more details:

http://developer.android.com/reference/android/content/BroadcastReceiver.html

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

This is what you want put it in onCreate method

BroadcastReceiver batteryReceiver = new BroadcastReceiver() 
        {
            int scale = -1;

            int level = -1;

            int voltage = -1;

            int temp = -1;

            @Override
            public void onReceive(Context context, Intent intent) 
            {
                level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

                scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

                temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);

                voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);

                Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
            }    
        };

            IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

            registerReceiver(batteryReceiver, filter);
Xar-e-ahmer Khan
  • 1,314
  • 15
  • 23