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 isBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
        }/*  ww w  .ja  v  a 2s . c o m*/
    }
    return false;
}

From source file:Main.java

private static String getProcessName(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> infos = activityManager.getRunningAppProcesses();
    int pid = Process.myPid();
    RunningAppProcessInfo myInfo = null;
    for (RunningAppProcessInfo info : infos) {
        if (info.pid == pid) {
            myInfo = info;//from www  .  j  av  a2s  .  com
            break;
        }
    }

    return myInfo == null ? null : myInfo.processName;
}

From source file:Main.java

/**
 * Gets cur process name.//from  w  w w .  ja va 2 s  .  c  om
 *
 * @return the cur process name
 */
public static String getCurProcessName() {
    int pid = android.os.Process.myPid();
    ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}

From source file:Main.java

public static boolean isNamedProcess(Context context, String processName) {
    if (context == null || TextUtils.isEmpty(processName)) {
        return false;
    }// w ww  .  j  ava  2 s. c  om

    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
    if (processInfoList == null) {
        return true;
    }

    for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid && processName.equalsIgnoreCase(processInfo.processName)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * is out app running in background ?/*from w w w .  jav  a  2s . co m*/
 * 
 * when system reboot, this method will return false although our app is not already running, so is this a bug?
 * 
 * @param context
 * @return true if in background; false if not.
 */
public static boolean isInBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND
                    || appProcess.importance == RunningAppProcessInfo.IMPORTANCE_SERVICE) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppInBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return true;
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            return appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
        }/*www . j  a va  2s. c om*/
    }
    return false;
}

From source file:Main.java

/**
 *///  w  w w .j a  va2 s  .  c  om
public static void clearEmptyTask(Context context) {
    ActivityManager activityManger = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
    if (list != null)
        for (int i = 0; i < list.size(); i++) {
            ActivityManager.RunningAppProcessInfo apinfo = list.get(i);
            String[] pkgList = apinfo.pkgList;
            if (apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY) {// >=300
                android.os.Process.killProcess(apinfo.pid);
                for (int j = 0; j < pkgList.length; j++) {
                    activityManger.killBackgroundProcesses(pkgList[j]);
                }
            }
        }
}

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)) {
            Log.i("NotificationLaunch",
                    String.format("the %s is running, isAppAlive return true", packageName));
            return true;
        }//from  w  w  w .ja v  a  2 s .  co  m
    }
    Log.i("NotificationLaunch", String.format("the %s is not running, isAppAlive return false", packageName));
    return false;
}

From source file:Main.java

public static String getProcessName(Context appContext) {
    String currentProcessName = null;
    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid) {
            currentProcessName = processInfo.processName;
            break;
        }/*from w w  w.  j  ava  2 s.co  m*/
    }
    return currentProcessName;
}

From source file:Main.java

public static boolean isAppRunning(Context context, String packageName) {
    if (packageName == null)
        return false;

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(packageName)) {
            return true;
        }/*from  w  ww .  jav a2  s .  c o  m*/
    }
    return false;
}