Search code examples
androidbatteryusage-statistics

Android BatteryManager Alternatives


I'm trying to get battery stats in my application for some benchmarking. The wonderful BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER lets me query the device's micro-amps which is great. However, this was only introduced in API 21 so the number of devices this can reach is quite limited. Is there a compatible way to do something similar in lower versions of the APIs (down to 4.0)? I know BatteryManager.EXTRA_LEVEL will give me the percentage but that's really not fine-grained.


Solution

  • As far as I know, you can't do that with older public APIs. I worked on a system-level app that shows battery usage stats to a user. I based my code from Android Settings app and my users might need root access or the app has to be signed with platform key. You can look into PowerUsageDetail, BatteryInfo, and PowerUsageSummary classes. If you are lucky, you might get access to hidden APIs with JAVA reflection. Otherwise, you need platform key or root. The internal APIs change very often so it's quite a pain to use.

    There are also a couple tools that you might want to check out:

    • adb shell dumpsys batterystats
    • adb shell dumpsys batteryinfo
    • battery historian
    • adb shell bugreport

    Dumpsys source code

    Good luck!

    Edit:

    My old project might be useful for you.