2

For a test we are conducting we need to measure the power consumption, which our background android service uses.

Is it possible to get the power usage in mAH, which our app has been using from the moment we disconnect the power supply, in code (Java or Xamarin C#)? If yes, how.

This similar as in (android 6.0) Settings->Apps->MyApp->Battery, section Computed power use.

Thanks in advance!

Edit

We have looked into the Battery Historian but we are deploying this test app on a few devices, which we cannot dumpsys each day to pull statistics from. Thats why we are making a API backend and a small app to measure our usage.

AntonK
  • 21
  • 1
  • 4
  • Have you seen: https://developer.android.com/studio/profile/battery-historian-charts.html#FilteringBatterystats – Morrison Chang Sep 20 '16 at 15:41
  • 1
    @MorrisonChang, Yes I have! We need only application stats Plus we will deploy this test app on a few test cases which will not be connected via a profiler. – AntonK Sep 21 '16 at 06:15
  • 2
    @mkierc Don't think you have understand my question: In code Not via any other app – AntonK Sep 21 '16 at 06:16
  • Consider this answer to access dumpsys (and therefore battery historian) if you aren't rooting the devices: http://stackoverflow.com/a/24104993/295004 otherwise from what I understand access that data is at the system level only (i.e. access via a rooted device or custom rom). – Morrison Chang Sep 21 '16 at 06:24
  • @MorrisonChang, Are you telling me, that the mAh which the app is using is not accessible by the app it self? Then how was the creator of [this paper](http://web.eecs.umich.edu/~zmao/Papers/powertutor_cases2010.pdf) able to make a tool which can get the power usage of other apps? – AntonK Sep 21 '16 at 06:38
  • Regarding the paper and tool - you'll need root (and its model is from 2010) - see: http://stackoverflow.com/a/12582179/295004 and http://stackoverflow.com/a/37745306/295004 Speculation - there are papers out there like: https://eprint.iacr.org/2015/616.pdf which show that battery info can leak privacy related info. Google may want to keep it as a developer/adb only feature as it requires user interaction and on device users can check battery stats in settings. – Morrison Chang Sep 21 '16 at 07:12
  • @MorrisonChang, Thanks for the answer – AntonK Sep 22 '16 at 07:10
  • @MorrisonChang How are apps like AccuBattery able to get info both on per app power consumption, but total mAh discharged *and* charged? It is necessary to grant this permission in the settings (NO ROOT REQUIRED!), but once you do this, how does it get this info from Android? – Michael Sep 14 '18 at 03:15

1 Answers1

1

Yes, you can. Actually, Those power consumption value cannot be accessed by user since Google hidden batteryStatsHelper API in Android 4.0(probably not correct). But you can still use it if you programme in a source code environment, and have a rooted device.

Here's a tutorial to show you how to use Google Hidden API https://github.com/anggrayudi/android-hidden-api

Please check BatteryStatsHelper.java for more information.

Xudong Ran
  • 101
  • 1
  • 3