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:cn.whereyougo.framework.utils.PackageManagerUtil.java

/**
 * ????//from www. j a  v  a 2  s  . c o m
 * 
 * @param context
 */
public static boolean isAppOnForeground(final Context context) {
    ActivityManager activityManager = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = context.getApplicationContext().getPackageName();
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses != null) {
        for (RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static int gc(Context cxt) {
    //long i = getDeviceUsableMemory(cxt);
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null)
        for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;//from w  w  w.j a va  2  s  . c  o  m
            } catch (Exception e) {
                e.getStackTrace();
                continue;
            }
        }

    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null)
        for (RunningAppProcessInfo process : processList) {
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        e.getStackTrace();
                        continue;
                    }
                }
            }
        }
    return count;
}

From source file:Main.java

public static int gc(Context cxt) {
    //long i = getDeviceUsableMemory(cxt);
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null)
        for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;/*ww  w.java 2 s . c om*/
            } catch (Exception e) {
                e.getStackTrace();
                //continue;
            }
        }

    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null)
        for (RunningAppProcessInfo process : processList) {
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        e.getStackTrace();
                        //continue;
                    }
                }
            }
        }
    return count;
}

From source file:Main.java

/**
 * kill other process and services//from   ww  w. j  ava  2  s. c  om
 *
 * @param context
 * @return count of clean up process and services
 */
public static int gc(Context context) {
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null) {
        for (ActivityManager.RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }

    List<ActivityManager.RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null) {
        for (ActivityManager.RunningAppProcessInfo process : processList) {
            if (process.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        e.getStackTrace();
                    }
                }
            }
        }
    }
    return count;
}

From source file:com.nxp.ltsm.ltsmclient.tools.Utils.java

static String getForeGroundL(Context context) {
    final int PROCESS_STATE_TOP = 2;
    RunningAppProcessInfo currentInfo = null;
    Field field = null;/*  ww  w . j av a 2s.  c o m*/
    try {
        field = RunningAppProcessInfo.class.getDeclaredField("processState");
    } catch (Exception e) {
        e.printStackTrace();
    }
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appList = am.getRunningAppProcesses();
    for (RunningAppProcessInfo app : appList) {
        if (app.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && app.importanceReasonCode == 0) {
            Integer state = null;
            try {
                state = field.getInt(app);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (state != null && state == PROCESS_STATE_TOP) {
                currentInfo = app;
                break;
            }
        }
    }
    return currentInfo.processName;
}

From source file:com.fastbootmobile.encore.utils.Utils.java

public static String getAppNameByPID(Context context, int pid) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid) {
            return processInfo.processName;
        }/*from ww w.  j  a  v  a2 s  .  c  o  m*/
    }
    return "";
}

From source file:com.appassit.common.Utils.java

public static void clearBackMemory() {
    ActivityManager activityManager = (ActivityManager) SLAppication.getContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    int currentProcessId = android.os.Process.myPid();
    RunningAppProcessInfo runningAppProcessInfo = null;
    List<RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
    if (runningAppProcessInfos != null) {
        for (int i = 0; i < runningAppProcessInfos.size(); ++i) {
            runningAppProcessInfo = runningAppProcessInfos.get(i);
            // RunningAppProcessInfo.IMPORTANCE_SERVICE
            // ?/* w w  w  . j a v  a  2 s.  com*/
            // RunningAppProcessInfo.IMPORTANCE_VISIBLE
            // ???,????
            if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE
                    && runningAppProcessInfo.pid != currentProcessId) {
                String[] pkgList = runningAppProcessInfo.pkgList;
                for (int j = 0; j < pkgList.length; ++j) {
                    activityManager.killBackgroundProcesses(pkgList[j]);
                    // count++;
                }
            }
        }
    }
}

From source file:com.jungle.base.utils.MiscUtils.java

public static int getApplicationPid() {
    Context context = BaseApplication.getAppContext();
    String pkgName = getPackageName();

    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningList = manager.getRunningAppProcesses();

    for (ActivityManager.RunningAppProcessInfo info : runningList) {
        if (TextUtils.equals(pkgName, info.processName)) {
            return info.pid;
        }/*from www  .  j a  v  a2 s  .com*/
    }

    return 0;
}

From source file:com.jungle.base.utils.MiscUtils.java

public static List<Integer> getApplicationPidList() {
    Context context = BaseApplication.getAppContext();
    String pkgName = getPackageName();

    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningList = manager.getRunningAppProcesses();

    List<Integer> result = new ArrayList<>();
    for (ActivityManager.RunningAppProcessInfo info : runningList) {
        if (info.processName.contains(pkgName)) {
            result.add(info.pid);//  ww  w . ja  v  a2s  .c o m
        }
    }

    return result;
}

From source file:com.adjust.sdk.ActivityHandler.java

public static ActivityHandler getInstance(AdjustConfig adjustConfig) {
    if (adjustConfig == null) {
        AdjustFactory.getLogger().error("AdjustConfig missing");
        return null;
    }//from  ww w  .  j a  va2  s.  c  o m

    if (!adjustConfig.isValid()) {
        AdjustFactory.getLogger().error("AdjustConfig not initialized correctly");
        return null;
    }

    if (adjustConfig.processName != null) {
        int currentPid = android.os.Process.myPid();
        ActivityManager manager = (ActivityManager) adjustConfig.context
                .getSystemService(Context.ACTIVITY_SERVICE);

        if (manager == null) {
            return null;
        }

        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == currentPid) {
                if (!processInfo.processName.equalsIgnoreCase(adjustConfig.processName)) {
                    AdjustFactory.getLogger().info("Skipping initialization in background process (%s)",
                            processInfo.processName);
                    return null;
                }
                break;
            }
        }
    }

    ActivityHandler activityHandler = new ActivityHandler(adjustConfig);
    return activityHandler;
}