Search in sources :

Example 56 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project superCleanMaster by joyoyao.

the class AppUtil method getRunningAppProcesses.

/**
 * 描述:获取运行的进程列表.
 *
 * @param context
 * @return
 */
public static List<AppProcessInfo> getRunningAppProcesses(Context context) {
    ActivityManager activityManager = null;
    List<AppProcessInfo> list = null;
    PackageManager packageManager = null;
    try {
        activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        packageManager = context.getApplicationContext().getPackageManager();
        list = new ArrayList<AppProcessInfo>();
        // 所有运行的进程
        List<RunningAppProcessInfo> appProcessList = activityManager.getRunningAppProcesses();
        ApplicationInfo appInfo = null;
        AppProcessInfo abAppProcessInfo = null;
        PackageInfo packageInfo = getPackageInfo(context);
        if (mProcessList != null) {
            mProcessList.clear();
        }
        mProcessList = getProcessRunningInfo();
        for (RunningAppProcessInfo appProcessInfo : appProcessList) {
            abAppProcessInfo = new AppProcessInfo(appProcessInfo.processName, appProcessInfo.pid, appProcessInfo.uid);
            appInfo = getApplicationInfo(context, appProcessInfo.processName);
            if (appInfo != null) {
                if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                    abAppProcessInfo.isSystem = true;
                } else {
                    abAppProcessInfo.isSystem = false;
                }
                Drawable icon = appInfo.loadIcon(packageManager);
                String appName = appInfo.loadLabel(packageManager).toString();
                abAppProcessInfo.icon = icon;
                abAppProcessInfo.appName = appName;
            } else {
                // :服务的命名
                if (appProcessInfo.processName.indexOf(":") != -1) {
                    appInfo = getApplicationInfo(context, appProcessInfo.processName.split(":")[0]);
                    Drawable icon = appInfo.loadIcon(packageManager);
                    abAppProcessInfo.icon = icon;
                }
                abAppProcessInfo.isSystem = true;
                abAppProcessInfo.appName = appProcessInfo.processName;
            }
            /*
                 * AbPsRow psRow = getPsRow(appProcessInfo.processName);
				 * if(psRow!=null){ abAppProcessInfo.memory = psRow.mem; }
				 */
            ProcessInfo processInfo = getMemInfo(appProcessInfo.processName);
            abAppProcessInfo.memory = processInfo.memory;
            abAppProcessInfo.cpu = processInfo.cpu;
            abAppProcessInfo.status = processInfo.status;
            abAppProcessInfo.threadsCount = processInfo.threadsCount;
            list.add(abAppProcessInfo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;
}
Also used : PackageInfo(android.content.pm.PackageInfo) ApplicationInfo(android.content.pm.ApplicationInfo) Drawable(android.graphics.drawable.Drawable) ProcessInfo(com.balaganovrocks.yourmasterclean.bean.ProcessInfo) AppProcessInfo(com.balaganovrocks.yourmasterclean.bean.AppProcessInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager) AppProcessInfo(com.balaganovrocks.yourmasterclean.bean.AppProcessInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) IOException(java.io.IOException) PackageManager(android.content.pm.PackageManager) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo)

Example 57 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project ZI by yixia.

the class ProcessHelper method isProcessRunning.

public static boolean isProcessRunning(Context ctx, String name) {
    ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> apps = am.getRunningAppProcesses();
    for (RunningAppProcessInfo app : apps) {
        if (app.processName.equals(name)) {
            return true;
        }
    }
    return false;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager)

Example 58 with RunningAppProcessInfo

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

the class AppUtils method isNamedProcess.

/**
 * whether this process is named with processName
 *
 * @param context     上下文
 * @param processName 进程名
 * @return 是否含有当前的进程
 */
public static boolean isNamedProcess(Context context, String processName) {
    if (context == null || TextUtils.isEmpty(processName)) {
        return false;
    }
    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;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager)

Example 59 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project react-native-push-notification by zo0r.

the class RNPushNotificationHelper method isApplicationInForeground.

public boolean isApplicationInForeground() {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    if (processInfos != null) {
        for (RunningAppProcessInfo processInfo : processInfos) {
            if (processInfo.processName.equals(context.getPackageName()) && processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && processInfo.pkgList.length > 0) {
                return true;
            }
        }
    }
    return false;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager)

Example 60 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project YhLibraryForAndroid by android-coco.

the class SystemUtils 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) {
                    LogUtils.e("======正在杀死包名:" + pkgName);
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        // 防止意外发生
                        e.getStackTrace();
                        continue;
                    }
                }
            }
        }
    }
    LogUtils.e("清理了" + (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)

Aggregations

RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)61 ActivityManager (android.app.ActivityManager)52 ArrayList (java.util.ArrayList)9 RunningServiceInfo (android.app.ActivityManager.RunningServiceInfo)8 IOException (java.io.IOException)8 IActivityManager (android.app.IActivityManager)6 Context (android.content.Context)6 Build (android.os.Build)6 RemoteException (android.os.RemoteException)6 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 Fingerprint (android.hardware.fingerprint.Fingerprint)5 MemoryInfo (android.os.Debug.MemoryInfo)5 SuppressLint (android.annotation.SuppressLint)4 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)4 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)4 Device (com.microsoft.appcenter.ingestion.models.Device)4