Example usage for android.app ActivityManager getRunningAppProcesses

List of usage examples for android.app ActivityManager getRunningAppProcesses

Introduction

In this page you can find the example usage for android.app ActivityManager getRunningAppProcesses.

Prototype

public List<RunningAppProcessInfo> getRunningAppProcesses() 

Source Link

Document

Returns a list of application processes that are running on the device.

Usage

From source file:Main.java

public static boolean isAppOnRunning(Context context, String packageName) {
    if (packageName == null || context == null) {
        return false;
    }//from  w  w w .  j a v a  2 s.c  o  m
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static String getProcessName(Context cxt, int pid) {
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
    if (runningApps == null) {
        return null;
    }/*from ww w  .  jav a 2 s  .com*/
    for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
        if (procInfo.pid == pid) {
            return procInfo.processName;
        }
    }
    return null;
}

From source file:Main.java

public static String getProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }//from  w  w  w.java 2 s  .  c  o m
    }
    return null;
}

From source file:Main.java

public static String getCurrentProcessName(Context pContext) {
    int pid = android.os.Process.myPid();
    ActivityManager mActivityManager = (ActivityManager) pContext.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }/*  www.j  a  v  a 2s  .c o m*/
    }
    return "";
}

From source file:Main.java

public static String getProcessName(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> raps = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo rap : raps) {
        if (rap != null && rap.pid == pid) {
            return rap.processName;
        }//w w  w  . j ava2s.c  o m
    }
    return null;
}

From source file:Main.java

/**
 * Get package name of the process id.// w  ww.  j  av  a 2s . c  om
 * @param context
 * @param pid
 * @return
 */
public static final String getPackageNameFromPid(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
    for (RunningAppProcessInfo info : processes) {
        if (info.pid == pid) {
            String[] packages = info.pkgList;
            if (packages.length > 0) {
                return packages[0];
            }
            break;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Checks if this application is foreground
 * //from www  . ja v a2s  .co  m
 * @param context Context to be examined
 * @return true if this application is running on the top; false otherwise
 */
public static boolean isContextForeground(Context context) {

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    int pid = getPid();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.pid == pid) {
            return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppOnForeground(Context context, String packageName) {
    if (packageName == null || context == null) {
        return false;
    }/*from w ww  .  j  a v  a  2 s .c o m*/
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

private static String getCurProcessName(Context context) {
    int pid = Process.myPid();
    ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }/* w  w  w.  j a  v a  2 s  .com*/
    }
    return null;
}

From source file:Main.java

public static boolean isAppAlive(Context context, String packageName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    for (int i = 0; i < processInfos.size(); i++) {
        if (processInfos.get(i).processName.equals(packageName)) {
            return true;
        }//from w  w w .  j  a v a 2s  .c om
    }
    return false;
}