Search in sources :

Example 6 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo 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 7 with RunningAppProcessInfo

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

the class SamplingLibrary method getRunningProcessInfo.

/**
	 * Populate running process info into the runningAppInfo WeakReference list, and return its value.
	 * @param context the Context
	 * @return the value of the runningAppInfo WeakReference list after setting it.
	 */
private static List<RunningAppProcessInfo> getRunningProcessInfo(Context context) {
    if (runningAppInfo == null || runningAppInfo.get() == null) {
        ActivityManager pActivityManager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> runningProcs = pActivityManager.getRunningAppProcesses();
        /*
			 * TODO: Is this the right thing to do? Remove part after ":" in
			 * process names
			 */
        for (RunningAppProcessInfo i : runningProcs) {
            if (i != null && i.processName != null) {
                int idx = i.processName.lastIndexOf(':');
                if (idx <= 0)
                    idx = i.processName.length();
                i.processName = i.processName.substring(0, idx);
            }
        }
        runningAppInfo = new WeakReference<List<RunningAppProcessInfo>>(runningProcs);
    }
    return runningAppInfo.get();
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ActivityManager(android.app.ActivityManager)

Example 8 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo 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 9 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo 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 10 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project android_frameworks_base by DirtyUnicorns.

the class FingerprintService method isForegroundActivity.

private boolean isForegroundActivity(int uid, int pid) {
    try {
        List<RunningAppProcessInfo> procs = ActivityManagerNative.getDefault().getRunningAppProcesses();
        int N = procs.size();
        for (int i = 0; i < N; i++) {
            RunningAppProcessInfo proc = procs.get(i);
            if (proc.pid == pid && proc.uid == uid && proc.importance == IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
    } catch (RemoteException e) {
        Slog.w(TAG, "am.getRunningAppProcesses() failed");
    }
    return false;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RemoteException(android.os.RemoteException) Fingerprint(android.hardware.fingerprint.Fingerprint)

Aggregations

RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)33 ActivityManager (android.app.ActivityManager)25 IActivityManager (android.app.IActivityManager)6 RemoteException (android.os.RemoteException)6 Fingerprint (android.hardware.fingerprint.Fingerprint)5 MemoryInfo (android.os.Debug.MemoryInfo)5 RunningServiceInfo (android.app.ActivityManager.RunningServiceInfo)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)4 ArrayList (java.util.ArrayList)4 PackageInfo (android.content.pm.PackageInfo)3 PackageManager (android.content.pm.PackageManager)3 IOException (java.io.IOException)3 SuppressLint (android.annotation.SuppressLint)2 Context (android.content.Context)2 ApplicationInfo (android.content.pm.ApplicationInfo)2 Drawable (android.graphics.drawable.Drawable)2 Build (android.os.Build)2 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)2 TestCrashException (com.microsoft.azure.mobile.crashes.model.TestCrashException)2 Device (com.microsoft.azure.mobile.ingestion.models.Device)2