3

I am trying to get the power consumption details made by each installed app in a list. But since I do not have root access, I know I won't be able to use adb shell commands to get battery usage info for all processes.

What I think I can do is get the uptime for each installed app, get the total time my battery has been disconnected (by saving timestamp when ACTION_POWER_DISCONNECTED happens), and calculating -

( app_runtime * 100 ) / total_time_battery_disconnected

However, I am unable to obtain the most basic info about an app -> how long it has been running ? I can use activeSince on RunningServices, but that is not the solution here.

The class ApplicationInfo does not provide any such methods either.

Is there any way to get this detail ? I see so many bloatware apps showing my the total consumption made by every app I have installed with the running since time also shown, so I know it is possible. But how ?

I looked into this article on Android Battery Life. But this is limited to Android internals.

Even tried UsageStats, but its not working, returning Zero when querying usage stats list.

1 Answers1

1

The UsageStatsManager should do what you are asking for. Not sure why it's not working, perhaps you need this in your manifest?

<uses-permission
      android:name="android.permission.PACKAGE_USAGE_STATS"
      tools:ignore="ProtectedPermissions" />

Here's a sample logging the UsageStats

long startTime = new GregorianCalendar(2016, 0, 1).getTimeInMillis();
long endTime = new GregorianCalendar(2017, 0, 1).getTimeInMillis();

UsageStatsManager usageStatsManager = (UsageStatsManager)context.getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime);

for (UsageStats us : queryUsageStats) {
    Log.d(TAG, us.getPackageName() + " = " + us.getTotalTimeInForeground());
}

This page has an example that walks you through the process of getting the persmission from the user. Lastly, here a full sample on GitHub.

Prior to API 21 - The SDK does not provide a mechanism to get application usage statistics. The only way to do it would be to poll the ActivityManager using the getRunningTasks or getRecentTasks functions. I'll direct you to CommonsWare's response to a similar question.

Community
  • 1
  • 1
Mike
  • 5,482
  • 2
  • 21
  • 25
  • Thanks but UsageManager is applicable only for API 21 and above. – Kunal Chawla May 16 '17 at 06:24
  • I updated my answer. Although, it's not good news, so maybe that's why you haven't responded to the update. – Mike May 18 '17 at 15:37
  • I changed my approach and instead of getting uptime for the running apps, I calculated the [memory consumed](https://developer.android.com/reference/android/app/ActivityManager.html#getProcessMemoryInfo(int[])) for each running service, and since I knew the available memory the system had, I calculated percentage of RAM being used by each process and ranked them in that order, which is basically what Android Developer Options gives us when we click on Running Services (in Settings). – Kunal Chawla May 19 '17 at 07:48