4

My app is runnning code bellow in a working thread every 5 seconds. But it returns a fixed value of -9223372036854775808/ Am I missiging anything?

    BatteryManager batteryManager =
            (BatteryManager)context.getSystemService(Context.BATTERY_SERVICE);

    mEnergyNWH = batteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
Edgard Lima
  • 387
  • 4
  • 12

2 Answers2

2

Android 4.4_r1 removed the possibility for apps to request for the BATTERY_INFO permission as stated in this commit.

You have to:

  1. ROOT your device
  2. Download and install the Xposed installer
  3. Install the “Framework" in the "Xposed Installer" and activate "Enable BatteryStats Permission" in "Modules".
  4. Reboot your device

Since now you have unlocked root permissions, you can access to BATTERY_STATS permission.

Nicholas Allio
  • 717
  • 2
  • 9
  • 28
1
  1. I'm not sure but you might required <uses-permission android:name="android.permission.BATTERY_STATS" /> permission.

  2. BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER is int not long. Try batteryManager.getIntProperty()

  3. Return the value of a battery property of long type If the platform does not provide the property queried, this value will be Long.MIN_VALUE

So maybe the property is just unavailable. If so, try on a different device.

shkschneider
  • 17,833
  • 13
  • 59
  • 112