how to get the battery usage details of installed apps?

无人久伴 提交于 2019-12-03 23:03:37

Sorry to disappoint you, but Android's power usage APIs are not public. so there is no official way to get what you want.

But you can use reflection to access some private members and methods and make use of them. You can also check out this. From here you can get some quantitive values for individual app's power consumption.

You can use that value to calculate the percentage for all apps.

Battery stats can be dumped from the device like this:

adb shell dumpsys batteryinfo

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.

Instead of using reflection to access some private members and methods, I would recommend you to program in source code environment, which allow you to use any private API.

If you also want to include the internal classes or methods, do the following:

  1. Go to /platforms/.
  2. Copy, paste and replace the downloaded hidden API file into this directory, e.g. android-23/android.jar.
  3. Change compileSdkVersion and targetSdkVersion to 23 (for example).
  4. Finally, rebuild your project.

https://github.com/anggrayudi/android-hidden-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!