7

In previous releases of Android I was able to get the CPU usage by using a top call

        String[] cmd = {
                "sh",
                "-c",
                "top -m 1000 -d 0 -n 1 | grep \"" + aAppPID + "\""};
        lProcess = Runtime.getRuntime().exec(cmd);
        BufferedReader lReader = new BufferedReader(new InputStreamReader(lProcess.getInputStream()));

It appears that Android 7 has changed the security or something to not allow this. Now, if attached to Android Studio it will give me a line with a CPU % of 1. If running a release version I am seeing that it does not return any lines.

Does anyone know if there is a Permission that can be added to allow this?

My best guess is that the permissions notes documented here have something to do with this problem.

https://developer.android.com/about/versions/nougat/android-7.0-changes.html#perm

Here is a link to another question that I posted a solution to get CPU usage on Pre-7 releases.

How do I get the total CPU usage of an application from /proc/pid/stat?

Community
  • 1
  • 1
Brian S
  • 3,096
  • 37
  • 55
  • is there any update on this issue? I am also seems to be stuck with this issue. – Amritesh Jul 20 '17 at 10:42
  • I don't think that there is going to be a fix for this. I think from a security stand point Google is on the path to locking things down tighter, and unless they create an API for this, we will have no access. – Brian S Jul 20 '17 at 11:58
  • It would seem that Google is deciding to take more away rather then give back access. https://stackoverflow.com/questions/46182041/get-cpu-usage-of-a-device-running-android-8-oreo – Brian S Sep 20 '17 at 13:00

1 Answers1

0

Now that we have moved onto Oreo, it seems that Google continues to take away this functionality as a security concern, to which I would be shocked if they opened up any sort of new API to replace it. See the following issue tracker with a couple of comments from someone at Google. Their best recommendation is to use /proc/self/stat instead of /proc/stat

https://issuetracker.google.com/issues/37140047

Brian S
  • 3,096
  • 37
  • 55