1

In My app, I'm using "ACTION_BATTERY_CHANGED" intent to find battery level of the Android device.

** Problem:**

consider If the device is having 1-20 as battery level, I need to show low battery icon in my application.I'm not able to show immediately

I have registered the broadcast receiver for ACTION_BATTERY_CHANGED Intent on app launch itself. Since this is System wide announcement, I am not able to get immediately.

Can I trigger Broadcast for ACTION_BATTERY_CHANGED Intent using SendBroadcast() on App Launch?

Is this a correct behavior?

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
kavie
  • 2,154
  • 4
  • 28
  • 53

2 Answers2

0

Best way of get battery level is

           String currentBatteryStatus = "Battery Percentage";
            int deviceStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int batteryLevel = (int) (((float) level / (float) scale) * 100.0f);

            if (deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
                currentBatteryStatus = currentBatteryStatus + " Charging : " + batteryLevel + " %";
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                currentBatteryStatus = currentBatteryStatus + " Discharging : " + batteryLevel + " %";
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_FULL) {
                currentBatteryStatus = currentBatteryStatus + " Battery Full : " + batteryLevel + " %";
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
                currentBatteryStatus = currentBatteryStatus + " Unknown : " + batteryLevel + " %";
            }

            if (deviceStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                currentBatteryStatus = currentBatteryStatus + " Not Charging : " + batteryLevel + " %";
            }

If you have any questions then ask me

PriyankVadariya
  • 809
  • 9
  • 14
-1

ACTION_BATTERY_CHANGED is sticky intent and as soon as you register a receiver for this broadcast you should be getting last modified value of it. It seems like its issue with your broadcast setup. You can do it like this:

    BroadcastReceiver powerReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                    Logger.e("Power", "Battrery NOW " + String.valueOf(level) + "%");
                    //Your logic here

                }
            };

            @Override
            public void onResume() {
                super.onResume();
                getBaseActivity().registerReceiver(powerReceiver,
                        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));    
            }

            @Override
            public void onPause() {
                super.onPause();
                getBaseActivity().unregisterReceiver(powerReceiver);
            }
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154