/** * @return {@code true} if this process is in the foreground. */ public static boolean isMyProcessInTheForeground() { try { return new AndroidAppProcess(android.os.Process.myPid()).foreground; } catch (Exception e) { log(e, "Error finding our own process"); } return false; }
/** * @return a list of <i>all</i> processes running on the device. */ public static List<AndroidProcess> getRunningProcesses() { List<AndroidProcess> processes = new ArrayList<>(); File[] files = new File("/proc").listFiles(); for (File file : files) { if (file.isDirectory()) { int pid; try { pid = Integer.parseInt(file.getName()); } catch (NumberFormatException e) { continue; } try { processes.add(new AndroidProcess(pid)); } catch (IOException e) { log(e, "Error reading from /proc/%d.", pid); // System apps will not be readable on Android 5.0+ if SELinux is enforcing. // You will need root access or an elevated SELinux context to read all files under /proc. } } } return processes; }
/** * @return a list of all running app processes on the device. */ public static List<AndroidAppProcess> getRunningAppProcesses() { List<AndroidAppProcess> processes = new ArrayList<>(); File[] files = new File("/proc").listFiles(); for (File file : files) { if (file.isDirectory()) { int pid; try { pid = Integer.parseInt(file.getName()); } catch (NumberFormatException e) { continue; } try { processes.add(new AndroidAppProcess(pid)); } catch (AndroidAppProcess.NotAndroidAppProcessException ignored) { } catch (IOException e) { log(e, "Error reading from /proc/%d.", pid); // System apps will not be readable on Android 5.0+ if SELinux is enforcing. // You will need root access or an elevated SELinux context to read all files under /proc. } } } return processes; }
log(e, "Error reading from /proc/%d.", pid);
uid = status().getUid(); AndroidProcesses.log("name=%s, pid=%d, uid=%d, foreground=%b, cpuacct=%s, cpu=%s", name, pid, uid, foreground, cpuacct.toString(), cpu.toString()); } else { uid = status().getUid(); AndroidProcesses.log("name=%s, pid=%d, uid=%d foreground=%b, cpuacct=%s, cpu=%s", name, pid, uid, foreground, cpuacct.toString(), cpu.toString()); AndroidProcesses.log("name=%s, pid=%d, uid=%d foreground=%b", name, pid, uid, foreground);