-1

for an App I need to monitor the Average battery current in micro amperes. I did it like described here: https://source.android.com/devices/tech/power/index.html#device-power

private long getPowerConsumption() {
    BatteryManager mBatteryManager;
    mBatteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
    long energy = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
    return energy;
}

But I'm getting a "Cannot resolve symbol BATTERY_SERVICE".

The context is a reference to an Activity.

What I'm doing wrong?

dju
  • 129
  • 4
  • 18
  • Did you `import android.os.ServiceManager;` ? – Paritosh May 07 '15 at 12:57
  • 2
    it looks like this was added in API 21 - make sure your project's tartgetSDK level is at least 21 – EJK May 07 '15 at 12:57
  • Are you sure the problem is not somewhere else in your code? The BATTERY_SERVICE in Context is public and static. It's possible that your IDE can't find it because there is another error in your code. – se_bastiaan May 07 '15 at 12:59

1 Answers1

1

BATTERY_SERVICE was added in API Level 21. Set your compileSdkVersion to 21, and ensure that you only go through this code on API Level 21+ devices.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491