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

/**
 * Checks if an app or process with the specified package name is running
 *
 * @param context/*from  w ww. j  av a 2  s  .  c o  m*/
 * @param packageName The package name of the app or process to check
 * @return <b>true</b> if running, <b>false</b> otherwise
 */
public static Boolean isRunning(Context context, String packageName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfo = activityManager.getRunningAppProcesses();
    for (int i = 0; i < processInfo.size(); i++) {
        if (processInfo.get(i).processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppRunningBackground(Context paramContext) {
    String pkgName = null;//w ww . j  a va  2 s.  co  m
    List<RunningAppProcessInfo> localList = null;
    if (paramContext != null) {
        pkgName = paramContext.getPackageName();
        ActivityManager localActivityManager = (ActivityManager) paramContext.getSystemService("activity");
        if (localActivityManager != null) {
            localList = localActivityManager.getRunningAppProcesses();
            if ((localList == null) || (localList.size() <= 0)) {
                return false;
            }
        }
    }

    for (Iterator<RunningAppProcessInfo> localIterator = localList.iterator(); localIterator.hasNext();) {
        RunningAppProcessInfo info = localIterator.next();
        if (info.processName.equals(pkgName) && info.importance != 100) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppRunningBackground(Context paramContext) {
    String pkgName = null;//from   www  . j a  va2s  .  c  o m
    List<RunningAppProcessInfo> localList = null;
    if (paramContext != null) {
        pkgName = paramContext.getPackageName();
        ActivityManager localActivityManager = (ActivityManager) paramContext.getSystemService("activity");
        if (localActivityManager != null) {
            localList = localActivityManager.getRunningAppProcesses();
            if ((localList == null) || (localList.size() <= 0)) {
                return false;
            }
        }
    }

    for (Iterator<RunningAppProcessInfo> localIterator = localList.iterator(); localIterator.hasNext();) {
        ActivityManager.RunningAppProcessInfo info = localIterator.next();
        if (info.processName.equals(pkgName) && info.importance != 100) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isProessRunning(Context context, String proessName) {
    boolean isRunning = false;
    android.app.ActivityManager am = (android.app.ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);

    List<android.app.ActivityManager.RunningAppProcessInfo> lists = am.getRunningAppProcesses();
    for (android.app.ActivityManager.RunningAppProcessInfo info : lists) {
        if (info.processName.equals(proessName)) {
            isRunning = true;/*from   w  w  w .j av  a 2s . c  o  m*/
            return isRunning;
        }
    }

    return isRunning;
}

From source file:Main.java

/**
 * @return null may be returned if the specified process not found
 *//*from  w w w  .  java  2s.  c  o m*/
public static String getProcessName(Context cxt, int pid) {
    android.app.ActivityManager am = (android.app.ActivityManager) cxt
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<android.app.ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
    if (runningApps == null) {
        return null;
    }
    for (android.app.ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
        if (procInfo.pid == pid) {
            return procInfo.processName;
        }

    }
    return null;
}

From source file:Main.java

/**
 * Returns {@code true} if the current process is main.
 *
 * @param context The instance of {@link android.content.Context}.
 * @return {@code true} if the current process is main.
 *///ww w  .  ja va2s  .co  m
public static boolean isMainProcess(@NonNull final Context context) {
    final int currentPid = android.os.Process.myPid();
    final ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses();

    String currentProcessName = null;
    for (RunningAppProcessInfo process : runningProcesses) {
        if (process.pid == currentPid) {
            currentProcessName = process.processName;
            break;
        }
    }

    return context.getPackageName().equals(currentProcessName);
}

From source file:Main.java

public static String getCurrentProcessName(Context context) {
    String currentProcName = "";
    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (manager == null) {
        return "";
    }// w w w .  j a  va2s .c  om

    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid) {
            currentProcName = processInfo.processName;
        }
    }

    return currentProcName;
}

From source file:Main.java

/**
 * Return <tt>true</tt> if the caller runs in a process dedicated to the Capptain service.<br/>
 * Return <tt>false</tt> otherwise, e.g. if it's the application process (even if the Capptain
 * service is running in it) or another process.<br/>
 * This method is useful when the <b>android:process</b> attribute has been set on the Capptain
 * service, if this method return <tt>true</tt>, application initialization must not be done in
 * that process. This method is used by {@link CapptainApplication}.
 * @param context the application context.
 * @return <tt>true</tt> if the caller is running in a process dedicated to the Capptain service,
 *         <tt>false</tt> otherwise.
 * @see CapptainApplication// w w  w  .java  2 s.  c o m
 */
public static boolean isInDedicatedCapptainProcess(Context context) {
    /* Get our package info */
    PackageInfo packageInfo;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SERVICES);
    } catch (Exception e) {
        /*
         * NameNotFoundException (uninstalling?) or in some rare scenario an undocumented
         * "RuntimeException: Package manager has died.", probably caused by a system app process
         * crash.
         */
        return false;
    }

    /* Get main process name */
    String mainProcess = packageInfo.applicationInfo.processName;

    /* Get embedded Capptain process name */
    String capptainProcess = null;
    if (packageInfo.services != null)
        for (ServiceInfo serviceInfo : packageInfo.services)
            if ("com.ubikod.capptain.android.service.CapptainService".equals(serviceInfo.name)) {
                capptainProcess = serviceInfo.processName;
                break;
            }

    /* If the embedded Capptain service runs on its own process */
    if (capptainProcess != null && !capptainProcess.equals(mainProcess)) {
        /* The result is to check if the current process is the capptain process */
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningAppProcessInfo rapInfo : activityManager.getRunningAppProcesses())
            if (rapInfo.pid == Process.myPid())
                return rapInfo.processName.equals(capptainProcess);
    }

    /* Otherwise capptain is not running in a separate process (or not running at all) */
    return false;
}

From source file:Main.java

/**
 * Return <tt>true</tt> if the caller runs in a process dedicated to the Engagement service.<br/>
 * Return <tt>false</tt> otherwise, e.g. if it's the application process (even if the Engagement
 * service is running in it) or another process.<br/>
 * This method is useful when the <b>android:process</b> attribute has been set on the Engagement
 * service, if this method return <tt>true</tt>, application initialization must not be done in
 * that process. This method is used by {@link EngagementApplication}.
 * @param context the application context.
 * @return <tt>true</tt> if the caller is running in a process dedicated to the Engagement
 *         service, <tt>false</tt> otherwise.
 * @see EngagementApplication/* ww w  . j  a  v  a2 s .c  om*/
 */
public static boolean isInDedicatedEngagementProcess(Context context) {
    /* Get our package info */
    PackageInfo packageInfo;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SERVICES);
    } catch (Exception e) {
        /*
         * NameNotFoundException (uninstalling?) or in some rare scenario an undocumented
         * "RuntimeException: Package manager has died.", probably caused by a system app process
         * crash.
         */
        return false;
    }

    /* Get main process name */
    String mainProcess = packageInfo.applicationInfo.processName;

    /* Get embedded Engagement process name */
    String engagementProcess = null;
    if (packageInfo.services != null)
        for (ServiceInfo serviceInfo : packageInfo.services)
            if (SERVICE_CLASS.equals(serviceInfo.name)) {
                engagementProcess = serviceInfo.processName;
                break;
            }

    /* If the embedded Engagement service runs on its own process */
    if (engagementProcess != null && !engagementProcess.equals(mainProcess)) {
        /* The result is to check if the current process is the engagement process */
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningAppProcessInfo rapInfo : activityManager.getRunningAppProcesses())
            if (rapInfo.pid == Process.myPid())
                return rapInfo.processName.equals(engagementProcess);
    }

    /* Otherwise engagement is not running in a separate process (or not running at all) */
    return false;
}

From source file:id.zelory.tanipedia.util.Utils.java

public static boolean isMyAppRunning(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    boolean run = false;
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            if (appProcess.processName.equals("id.zelory.tanipedia")) {
                run = true;/*  ww  w . j a v  a  2s.c om*/
                break;
            }
        }
    }

    return run;
}