Search in sources :

Example 1 with AndroidAppProcess

use of com.wenming.library.processutil.models.AndroidAppProcess in project AndroidProcess by wenmingvs.

the class ProcessManager method isMyProcessInTheForeground.

/**
     * @return {@code true} if this process is in the foreground.
     */
public static boolean isMyProcessInTheForeground() {
    List<AndroidAppProcess> processes = getRunningAppProcesses();
    int myPid = android.os.Process.myPid();
    for (AndroidAppProcess process : processes) {
        if (process.pid == myPid && process.foreground) {
            return true;
        }
    }
    return false;
}
Also used : AndroidAppProcess(com.wenming.library.processutil.models.AndroidAppProcess)

Example 2 with AndroidAppProcess

use of com.wenming.library.processutil.models.AndroidAppProcess in project AndroidProcess by wenmingvs.

the class ProcessManager method getRunningAppProcessInfo.

/**
     * Returns a list of application processes that are running on the device.
     * <p/>
     * <p><b>NOTE:</b> On Lollipop (SDK 22) this does not provide
     * {@link RunningAppProcessInfo#pkgList},
     * {@link RunningAppProcessInfo#importance},
     * {@link RunningAppProcessInfo#lru},
     * {@link RunningAppProcessInfo#importanceReasonCode},
     * {@link RunningAppProcessInfo#importanceReasonComponent},
     * {@link RunningAppProcessInfo#importanceReasonPid},
     * etc. If you need more process information try using
     * {@link #getRunningAppProcesses()} or {@link android.app.usage.UsageStatsManager}</p>
     *
     * @param ctx the application context
     * @return a list of RunningAppProcessInfo records, or null if there are no
     * running processes (it will not return an empty list).  This list ordering is not
     * specified.
     */
public static List<RunningAppProcessInfo> getRunningAppProcessInfo(Context ctx) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        List<AndroidAppProcess> runningAppProcesses = ProcessManager.getRunningAppProcesses();
        List<RunningAppProcessInfo> appProcessInfos = new ArrayList<>();
        for (AndroidAppProcess process : runningAppProcesses) {
            RunningAppProcessInfo info = new RunningAppProcessInfo(process.name, process.pid, null);
            info.uid = process.uid;
            // TODO: Get more information about the process. pkgList, importance, lru, etc.
            appProcessInfos.add(info);
        }
        return appProcessInfos;
    }
    ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    return am.getRunningAppProcesses();
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) AndroidAppProcess(com.wenming.library.processutil.models.AndroidAppProcess) ArrayList(java.util.ArrayList) ActivityManager(android.app.ActivityManager)

Example 3 with AndroidAppProcess

use of com.wenming.library.processutil.models.AndroidAppProcess in project AndroidProcess by wenmingvs.

the class ProcessManager method getRunningAppProcesses.

/**
     * @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;
}
Also used : AndroidAppProcess(com.wenming.library.processutil.models.AndroidAppProcess) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 4 with AndroidAppProcess

use of com.wenming.library.processutil.models.AndroidAppProcess in project AndroidProcess by wenmingvs.

the class ProcessManager method getRunningForegroundApps.

/**
     * Get a list of user apps running in the foreground.
     *
     * @param ctx the application context
     * @return a list of user apps that are in the foreground.
     */
public static List<AndroidAppProcess> getRunningForegroundApps(Context ctx) {
    List<AndroidAppProcess> processes = new ArrayList<>();
    File[] files = new File("/proc").listFiles();
    PackageManager pm = ctx.getPackageManager();
    for (File file : files) {
        if (file.isDirectory()) {
            int pid;
            try {
                pid = Integer.parseInt(file.getName());
            } catch (NumberFormatException e) {
                continue;
            }
            try {
                AndroidAppProcess process = new AndroidAppProcess(pid);
                if (process.foreground && // ignore system processes. First app user starts at 10000.
                (process.uid < 1000 || process.uid > 9999) && // ignore processes that are not running in the default app process.
                !process.name.contains(":") && // Ignore processes that the user cannot launch.
                pm.getLaunchIntentForPackage(process.getPackageName()) != null) {
                    processes.add(process);
                }
            } 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;
}
Also used : PackageManager(android.content.pm.PackageManager) AndroidAppProcess(com.wenming.library.processutil.models.AndroidAppProcess) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Aggregations

AndroidAppProcess (com.wenming.library.processutil.models.AndroidAppProcess)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 IOException (java.io.IOException)2 ActivityManager (android.app.ActivityManager)1 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)1 PackageManager (android.content.pm.PackageManager)1