1

So for my app I want to monitor the battery level, even when my app is closed, and then send a notification once the battery goes below a certain percentage. I've know how to create a background service like this, but I'm not sure how I would use one to monitor battery levels. Would I register a BroadcastReceiver in the Manifest file, or should I do it dynamically? Also, I'm concerned that the BroadcastReceiver would be terminated by the OS after it receives its first update.

Calvin Li
  • 2,614
  • 3
  • 17
  • 25
  • http://stackoverflow.com/questions/24532934/checking-battery-level-in-the-background-with-a-service see walking food's solution – Kriptite May 14 '15 at 07:23

1 Answers1

6

You MUST register it dynamically.

Doing so in a service would probably be a good bet. This is what I've used:

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

...

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {
      // TODO Auto-generated method stub
        //this will give you battery current status

    try{
      int level = intent.getIntExtra("level", 0);
      int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
      int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
      int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

      String BStatus = "No Data";
      if (status == BatteryManager.BATTERY_STATUS_CHARGING){BStatus = "Charging";}
      if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){BStatus = "Discharging";}
      if (status == BatteryManager.BATTERY_STATUS_FULL){BStatus = "Full";}
      if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){BStatus = "Not Charging";}
      if (status == BatteryManager.BATTERY_STATUS_UNKNOWN){BStatus = "Unknown";}

      int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
      String BattPowerSource = "No Data";
      if (chargePlug == BatteryManager.BATTERY_PLUGGED_AC){BattPowerSource = "AC";}
      if (chargePlug == BatteryManager.BATTERY_PLUGGED_USB){BattPowerSource = "USB";}

      String BattLevel = String.valueOf(level);

      int BHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
      String BatteryHealth = "No Data";
      if (BHealth == BatteryManager.BATTERY_HEALTH_COLD){BatteryHealth = "Cold";}
      if (BHealth == BatteryManager.BATTERY_HEALTH_DEAD){BatteryHealth = "Dead";}
      if (BHealth == BatteryManager.BATTERY_HEALTH_GOOD){BatteryHealth = "Good";}
      if (BHealth == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){BatteryHealth = "Over-Voltage";}
      if (BHealth == BatteryManager.BATTERY_HEALTH_OVERHEAT){BatteryHealth = "Overheat";}
      if (BHealth == BatteryManager.BATTERY_HEALTH_UNKNOWN){BatteryHealth = "Unknown";}
      if (BHealth == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){BatteryHealth = "Unspecified Failure";}

      //Do whatever with the data here


    } catch (Exception e){
        Log.v(TAG, "Battery Info Error");
    }
    }
  };
Di Vero Labs
  • 344
  • 1
  • 4
  • 1
    Do you know why I MUST do it dynamically? – Calvin Li Sep 25 '13 at 06:41
  • http://developer.android.com/reference/android/os/BatteryManager.html - "You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers. " – Di Vero Labs Sep 25 '13 at 09:38
  • 2
    This code only runs once when i start the app....i need it to keep running...how to do that? – Aradhna May 21 '14 at 04:41
  • Try using an AlarmManager – Alex Feb 09 '15 at 10:05