4

Background

Starting with some Android version (don't remember which), Android has the ability to show battery stats and mobile-data-usage (3G...) of the installed apps, so that the user will be aware of such a thing and will be able to think if the app should stay or not.

I'm talking about those:

enter image description here

The question

I've seen some apps on the play store that somehow fetch this data and show it to the user.

How do they do it?

How can I get the battery stats and mobile data usage of a specific app?

Is there a tutorial about this?

The only thing I've found is getting the status of the battery (charging/not charging,...).

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    There is no documented and supported API for the battery usage. It is also blocked as of Android 4.4 -- only system apps can hack their way into the OS to try to glean this data. And the data is not especially good in the first place. There is no documented and supported API for historical data usage either, though you are welcome to collect that data yourself via `TrafficStats`. – CommonsWare Apr 26 '14 at 14:21
  • Well that's too bad. Why did they do it? Is it the same for both kinds of stats (battery and data usage)? Is there a tutorial about how to use "TrafficStats" ? Also, is there at least an intent I can use to open each of the screens I've shown? – android developer Apr 26 '14 at 14:26
  • 1
    "Why did they do it?" -- I do not know what "it" is, and you would would have to ask "they" your question. "Is it the same for both kinds of stats" -- I do not know what "it" is. "Is there a tutorial about how to use "TrafficStats" ?" -- probably, but asking for off-site resources is off-topic nowadays for StackOverflow. "is there at least an intent I can use to open each of the screens I've shown?" -- I don't see one [on `Settings`](https://developer.android.com/reference/android/provider/Settings.html), which is where most of those reside, though they could be elsewhere. – CommonsWare Apr 26 '14 at 14:29
  • "it" is the issue you have talked about (not being able to get the stats, and deprecating what was available before). About intents, I remember that in old Android versions (I think 2.3) I could simply look at the log and see exactly which intent was fired whenever I opened an app. Is it possible to somehow get how to open those settings screens this way ? – android developer Apr 26 '14 at 14:36
  • I see there is some intent about battery, here: http://stackoverflow.com/a/9530965/878126 , but I can't find one for a specific app, and I can't find anything about mobile data usage. I've found how to get to the main data-usage screen, but it is a bit hack-y , here: http://stackoverflow.com/a/21128684/878126 – android developer Apr 26 '14 at 14:39
  • 1
    In terms of deprecation, they changed the `protectionLevel` of the relevant permission and AFAIK have not stated why. In terms of not being able to get the stats, I have no idea. In terms of examining LogCat, you may be able to find something that way, though how many devices it will work on remains to be seen, and whatever you find via that approach could easily break in the future. In terms of `ACTION_POWER_USAGE_SUMMARY`, that should work on most devices, though I am not sure why they put that one in `Intent` instead of `Settings`. – CommonsWare Apr 26 '14 at 14:41
  • Why should "ACTION_POWER_USAGE_SUMMARY" work only on some devices? it seems to be available ever since API 4... – android developer Apr 26 '14 at 15:20
  • 1
    Generally speaking, those `Intent` actions leading to the Settings app are not guaranteed to work. They call that out explicitly on some actions, but all are somewhat risky. Bear in mind that you are invoking a third-party app of an open source project when you use those actions. Device manufacturers routinely modify the Settings app, and they occasionally break things. Moreover, bear in mind that the *user* may not have access to that screen, courtesy of Android 4.3+'s restricted profiles on tablets. Hence, always assume possible failure. – CommonsWare Apr 26 '14 at 15:24
  • This is even weirder. Why would manufacturers remove such a useful feature ? It's the battery usage... Perhaps their apps drain the battery, so they don't want it to be found? – android developer Apr 26 '14 at 15:32
  • 1
    "Why would manufacturers remove such a useful feature ?" -- manufacturers are more likely to accidentally or intentionally remove the manifest action than to actually remove the screen entirely. The user may still be able to get to battery information, but they may not be able to jump to it from an arbitrary third-party app. – CommonsWare Apr 26 '14 at 15:55
  • I see. It seems using any kind of Intent on Android requires try-catch or querying whether any app can handle the intent... That's the disadvantage of being open source and the free intergration of apps, I guess. Why didn't you put all of this in an answer, so that I could tick it? – android developer Apr 26 '14 at 16:15
  • Technically, none of this answers the question. There are apparently undocumented and unsupported ways of getting at this stuff, as other apps demonstrate. While I wouldn't recommend that anyone use them, and I do not know how to get at that undocumented stuff, it's clearly possible to do. Even if we consider "you're not supposed to do that" as being an answer worthy of your acceptance, it'll just collect downvotes by people who don't like being told "no". Most of this comment thread was on the tangent of launching those Settings screens, and that's not part of your original question. – CommonsWare Apr 26 '14 at 16:21
  • yes, I agree, but it's still some kind of answer, and you could just say that even though there are ways to do it, they aren't supported and might be risky to be used. – android developer Apr 26 '14 at 16:30
  • @CommonsWare Is it possible to get the battery information from an adb command like "adb shell dumpsys batterystats" (or "adb shell dumpsys batteryinfo" for pre-kitkat) ? I've tried reading its output but it has huge amount of data, and I can't find the appropriate thing to read from there. I've noticed "Time on battery" and "Total run time", but when I calculated the percentage (using the total time), it doesn't match the numbers of the official battery management screen of Android. I also can't find information about reading this command's output correctly. – android developer Aug 29 '14 at 15:49
  • AFAIK, **`adb shell dumpsys`** works if run from your app, but that behavior is undocumented and unsupported. The `batterystats` flavor hopefully will be documented more once the L Developer Preview ships as a production Android version. Unfortunately, much of what `dumpsys` dumps is not well documented. :-( – CommonsWare Aug 29 '14 at 16:11
  • @CommonsWare Do you think a rooted device is needed for this? I see that the permission needed for it is "DUMP" (info here: http://developer.android.com/reference/android/Manifest.permission.html#DUMP) and that it's in the group of "DEVELOPMENT_TOOLS" (found by running "adb shell pm list permissions -f -g" ) , so if it's the same as others that I see there, it probably does need root. – android developer Aug 29 '14 at 16:29
  • That's very possible. I don't run shell commands from Android apps, so I have limited experience in this area. My apologies. – CommonsWare Aug 29 '14 at 16:36
  • @CommonsWare I see. It's just that I'm thinking to add this feature to sort apps by battery usage on my app (here: https://play.google.com/store/apps/details?id=com.lb.app_manager ) , but for now I just add a link to the battery stats of the device (if available). – android developer Aug 29 '14 at 20:09

0 Answers0