Search in sources :

Example 1 with ProcessInfo

use of com.yzy.supercleanmaster.bean.ProcessInfo 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.yzy.supercleanmaster.bean.ProcessInfo) AppProcessInfo(com.yzy.supercleanmaster.bean.AppProcessInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager) AppProcessInfo(com.yzy.supercleanmaster.bean.AppProcessInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) IOException(java.io.IOException) PackageManager(android.content.pm.PackageManager) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo)

Example 2 with ProcessInfo

use of com.yzy.supercleanmaster.bean.ProcessInfo in project superCleanMaster by joyoyao.

the class AppUtil method getMemInfo.

/**
     * 描述:根据进程ID获取CPU和内存信息.
     *
     * @param pid
     * @return
     */
public static ProcessInfo getMemInfo(int pid) {
    ProcessInfo process = new ProcessInfo();
    if (mProcessList == null) {
        mProcessList = getProcessRunningInfo();
    }
    String tempPidString = "";
    int tempPid = 0;
    int count = mProcessList.size();
    for (int i = 0; i < count; i++) {
        String[] item = mProcessList.get(i);
        tempPidString = item[0];
        if (tempPidString == null) {
            continue;
        }
        // AbLogUtil.d(AbAppUtil.class, "##"+item[9]+",PID:"+tempPid);
        tempPid = Integer.parseInt(tempPidString);
        if (tempPid == pid) {
            // AbLogUtil.d(AbAppUtil.class,
            // "##"+item[9]+","+process.memory);
            // Process ID
            process.pid = Integer.parseInt(item[0]);
            // CPU
            process.cpu = item[2];
            // S
            process.status = item[3];
            // thread
            process.threadsCount = item[4];
            // Mem
            long mem = 0;
            if (item[6].indexOf("M") != -1) {
                mem = Long.parseLong(item[6].replace("M", "")) * 1000 * 1024;
            } else if (item[6].indexOf("K") != -1) {
                mem = Long.parseLong(item[6].replace("K", "")) * 1000;
            } else if (item[6].indexOf("G") != -1) {
                mem = Long.parseLong(item[6].replace("G", "")) * 1000 * 1024 * 1024;
            }
            process.memory = mem;
            // UID
            process.uid = item[8];
            // Process Name
            process.processName = item[9];
            break;
        }
    }
    return process;
}
Also used : ProcessInfo(com.yzy.supercleanmaster.bean.ProcessInfo) AppProcessInfo(com.yzy.supercleanmaster.bean.AppProcessInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo)

Example 3 with ProcessInfo

use of com.yzy.supercleanmaster.bean.ProcessInfo in project superCleanMaster by joyoyao.

the class AppUtil method getMemInfo.

// /**
// *
// * 描述:执行PS.
// */
// public static List<AbPsRow> ps() {
// List<AbPsRow> psRowlist = new ArrayList<AbPsRow>();
// String ps = runScript("ps");
// String[] lines = ps.split("\n");
// psRowlist = new ArrayList<AbPsRow>();
// for (String line : lines) {
// AbPsRow row = new AbPsRow(line);
// if (row.pid != null) psRowlist.add(row);
// }
// return psRowlist;
// }
//
// /**
// *
// * 描述:获得这个进程的ps信息.
// * @param processName
// * @return
// */
// public static AbPsRow getPsRow(String processName) {
// List<AbPsRow> psRowlist = ps();
// for (AbPsRow row : psRowlist) {
// if (processName.equals(row.cmd)) {
// return row;
// }
// }
// return null;
// }
/**
     * 描述:根据进程名获取CPU和内存信息.
     *
     * @param processName
     * @return
     */
public static ProcessInfo getMemInfo(String processName) {
    ProcessInfo process = new ProcessInfo();
    if (mProcessList == null) {
        mProcessList = getProcessRunningInfo();
    }
    String processNameTemp = "";
    for (Iterator<String[]> iterator = mProcessList.iterator(); iterator.hasNext(); ) {
        String[] item = (String[]) iterator.next();
        processNameTemp = item[9];
        // "##"+item[9]+",NAME:"+processNameTemp);
        if (processNameTemp != null && processNameTemp.equals(processName)) {
            // AbLogUtil.d(AbAppUtil.class,
            // "##"+item[9]+","+process.memory);
            // Process ID
            process.pid = Integer.parseInt(item[0]);
            // CPU
            process.cpu = item[2];
            // S
            process.status = item[3];
            // thread
            process.threadsCount = item[4];
            // Mem
            long mem = 0;
            if (item[6].indexOf("M") != -1) {
                mem = Long.parseLong(item[6].replace("M", "")) * 1000 * 1024;
            } else if (item[6].indexOf("K") != -1) {
                mem = Long.parseLong(item[6].replace("K", "")) * 1000;
            } else if (item[6].indexOf("G") != -1) {
                mem = Long.parseLong(item[6].replace("G", "")) * 1000 * 1024 * 1024;
            }
            process.memory = mem;
            // UID
            process.uid = item[8];
            // Process Name
            process.processName = item[9];
            break;
        }
    }
    if (process.memory == 0) {
        L.d(AppUtil.class, "##" + processName + ",top -n 1未找到");
    }
    return process;
}
Also used : ProcessInfo(com.yzy.supercleanmaster.bean.ProcessInfo) AppProcessInfo(com.yzy.supercleanmaster.bean.AppProcessInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo)

Aggregations

RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)3 AppProcessInfo (com.yzy.supercleanmaster.bean.AppProcessInfo)3 ProcessInfo (com.yzy.supercleanmaster.bean.ProcessInfo)3 ActivityManager (android.app.ActivityManager)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 Drawable (android.graphics.drawable.Drawable)1 IOException (java.io.IOException)1