Using this code, I can find the battery level of the phone:
int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float percent = (level / (float)scale) * 100;
But how do I get the battery usage details of apps installed on phone, including that of the screen, as shown in the image below?
Measuring the power consumption for an specific app or for a specific part of the phones hardware is not that easy. From the public Google Developer API it is not possible to do this on an easy way. There are other ways which are quiet complicated. For every Android smart-phone Google requires the manufacturer to add a so called power profile XML to an device. They look like this
<item name="screen.on">0.1</item> <!-- ~200mA -->
<item name="screen.full">0.1</item> <!-- ~300mA -->
<item name="bluetooth.active">0.1</item> <!-- Bluetooth data transfer, ~10mA -->
<item name="bluetooth.on">0.1</item> <!-- Bluetooth on & connectable, but not connected, ~0.1mA -->
<item name="wifi.on">0.1</item> <!-- ~3mA -->
<item name="wifi.active">0.1</item> <!-- WIFI data transfer, ~200mA -->
<item name="wifi.scan">0.1</item>
They tell you how much current each hardware state of a component consumes (e.g. Wifi has three states, the CPU has 10 etc). Now Android is Linux based and this allows you the check the current state of each hardware component. For example the file
/sys/devices/virtual/leds/lcd-backlight/brightness
gives you the current state of your LCD (notice that for this no root access is required). For every hardware component such an path exists. If you now want to exactly measure how much energy an app consumes, you need to write a service which monitors whenever the app is active or not and what are the current hardware states. With this information you can compute the energy consumption.
There also exists an app which is open source, it is called PowerTutor (here is th Play-Store link and her you'll find the source code. It does exactly what I just explained. They also publish a scientific paper on how accurate their approach is (it does a good job).
Now comes the problem. Some years ago I had the problem of measuring the power consumption of some apps. A customer wanted us to evaluate their exact power consumption. In the end we found out that the power profiles on which the analysis is based are not accurate enough. It seemed that the manufacturers are just putting some values in the XML which are not even related to the device. We had over 100 different devices from 20 different brands only a few of them seemed to have values in their XML which were related to the device.
Finally I can say, don't spend to much time on measuring the real power consumption on the software side, it's not worth the effort. Use a real Multimeter and measure the hardware directly.
Battery stats can be dumped from the device like this for API >= 19:
adb shell dumpsys batterystats
For API < 19, use:
adb shell dumpsys batteryinfo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With