Search in sources :

Example 1 with RunningServiceInfo

use of android.app.ActivityManager.RunningServiceInfo in project carat by amplab.

the class SamplingLibrary method getRunningAppInfo.

public static List<ProcessInfo> getRunningAppInfo(Context c) {
    List<RunningAppProcessInfo> runningProcs = getRunningProcessInfo(c);
    List<RunningServiceInfo> runningServices = getRunningServiceInfo(c);
    List<ProcessInfo> l = new ArrayList<ProcessInfo>();
    if (runningProcs != null) {
        for (RunningAppProcessInfo pi : runningProcs) {
            if (pi == null)
                continue;
            ProcessInfo item = new ProcessInfo();
            item.setImportance(CaratApplication.importanceString(pi.importance));
            item.setPId(pi.pid);
            item.setPName(pi.processName);
            l.add(item);
        }
    }
    if (runningServices != null) {
        for (RunningServiceInfo pi : runningServices) {
            if (pi == null)
                continue;
            ProcessInfo item = new ProcessInfo();
            item.setImportance(pi.foreground ? "Foreground app" : "Service");
            item.setPId(pi.pid);
            item.setPName(pi.clientPackage);
            l.add(item);
        }
    }
    return l;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ArrayList(java.util.ArrayList) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)

Example 2 with RunningServiceInfo

use of android.app.ActivityManager.RunningServiceInfo in project Lazy by l123456789jy.

the class AppUtils method gc.

/**
     * 清理后台进程与服务
     *
     * @param context 应用上下文对象context
     * @return 被清理的数量
     */
public static int gc(Context context) {
    long i = getDeviceUsableMemory(context);
    // 清理掉的进程数
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    // 获取正在运行的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++;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }
    // 获取正在运行的进程列表
    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null) {
        for (RunningAppProcessInfo process : processList) {
            // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                // pkgList 得到该进程下运行的包名
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    if (DEBUG) {
                    }
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        // 防止意外发生
                        e.getStackTrace();
                    }
                }
            }
        }
    }
    if (DEBUG) {
    }
    return count;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ActivityManager(android.app.ActivityManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) CertificateException(java.security.cert.CertificateException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with RunningServiceInfo

use of android.app.ActivityManager.RunningServiceInfo in project Lazy by l123456789jy.

the class AppUtils method isServiceRunning.

/**
     * 检测服务是否运行
     *
     * @param context   上下文
     * @param className 类名
     * @return 是否运行的状态
     */
public static boolean isServiceRunning(Context context, String className) {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
    for (RunningServiceInfo si : servicesList) {
        if (className.equals(si.service.getClassName())) {
            isRunning = true;
        }
    }
    return isRunning;
}
Also used : RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ActivityManager(android.app.ActivityManager)

Example 4 with RunningServiceInfo

use of android.app.ActivityManager.RunningServiceInfo in project KJFrameForAndroid by kymjs.

the class SystemTool method gc.

/**
     * 清理后台进程与服务
     * 
     * @param cxt
     *            应用上下文对象context
     * @return 被清理的数量
     */
public static int gc(Context cxt) {
    long i = getDeviceUsableMemory(cxt);
    // 清理掉的进程数
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    // 获取正在运行的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++;
            } catch (Exception e) {
                e.getStackTrace();
                continue;
            }
        }
    // 获取正在运行的进程列表
    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null)
        for (RunningAppProcessInfo process : processList) {
            // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                // pkgList 得到该进程下运行的包名
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    KJLoger.debug("======正在杀死包名:" + pkgName);
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        // 防止意外发生
                        e.getStackTrace();
                        continue;
                    }
                }
            }
        }
    KJLoger.debug("清理了" + (getDeviceUsableMemory(cxt) - i) + "M内存");
    return count;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ActivityManager(android.app.ActivityManager) SuppressLint(android.annotation.SuppressLint) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Example 5 with RunningServiceInfo

use of android.app.ActivityManager.RunningServiceInfo in project LshUtils by SenhLinsh.

the class AppUtils method gc.

/**
     * 清理后台进程与服务
     *
     * @return 被清理的数量
     */
public static int gc(Context context) {
    long i = getDeviceUsableMemory(context);
    // 清理掉的进程数
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    // 获取正在运行的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++;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }
    // 获取正在运行的进程列表
    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null) {
        for (RunningAppProcessInfo process : processList) {
            // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                // pkgList 得到该进程下运行的包名
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    if (DEBUG) {
                    }
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        // 防止意外发生
                        e.getStackTrace();
                    }
                }
            }
        }
    }
    if (DEBUG) {
    }
    return count;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ActivityManager(android.app.ActivityManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Aggregations

RunningServiceInfo (android.app.ActivityManager.RunningServiceInfo)15 ActivityManager (android.app.ActivityManager)14 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)3 ComponentName (android.content.ComponentName)2 SuppressLint (android.annotation.SuppressLint)1 CancellablePreference (com.android.settings.CancellablePreference)1 OnCancelListener (com.android.settings.CancellablePreference.OnCancelListener)1 ProcessInfo (edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CertificateException (java.security.cert.CertificateException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1