7

i want to detect battery change at every percentage. I am able to detect battery level once with broadcast receiver. But its not updating value automatically when % change from 66 to 67.

Here is my code.

   private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                Log.i("Battery CHANGED******",":: IN RECEIVER");

                int rawlevel = intent.getIntExtra("level", -1);
                int scale = intent.getIntExtra("scale", -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                btnConsumer.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);

    }

Added permission

 <uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>

It detect 66 but when it change 65 or 67 it will not update value. or not display log too

I want to make it like i can get every change.

Thanks in advance.

jimmym715
  • 1,512
  • 1
  • 16
  • 25
djk
  • 3,671
  • 9
  • 31
  • 40

2 Answers2

16

First, you do not need the BATTERY_STATS permission.

Second, the very first line of your onReceive() unregisters your BroadcastReceiver, thereby preventing you from getting any further updates.

Third, there is no guarantee that you will have battery information delivered to you for each and every 1% change. Many Motorola phones, for example, only send ACTION_BATTERY_CHANGED every 10% until the battery gets low. The decision on how frequently to broadcast ACTION_BATTERY_CHANGED is up to the device manufacturer, not you.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so for a dashcam type app (to stop recording if `1%` percentage is left on a device) it's better to get this value manually from battery manager instead of using `ACTION_BATTERY_CHANGED` events? – user924 Apr 08 '21 at 12:14
  • 1
    @user924: Note that this question and answer are 9 years old at this point. Back then, what you describe ("get this value manually from battery manager") did not exist. Now it does, and my guess is that you are correct -- polling the `BatteryManager` occasionally to check the remaining charge has a chance of being more regular than are these broadcasts. – CommonsWare Apr 08 '21 at 12:26
0

You don't need that BATTERY_STATS permission

and you don't need to unregister the receiver in onReceive method because if you do so your receiver will just run once and not for the second time. So just register the receiver and do what you want to do in onReceive method.

if your phone is on charging state it will continuosly change the battery state and it will update the receiver many time in one percent