ActivityManager

前言

本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括进程(Process)等、应用程序/包、服务(Service)、任务(Task)信息。

  • ActivityManager.MemoryInfo 获取全局的内存使用信息
    long availMem 系统可用内存
    long totalMem 总内存
    long threshold 低内存阀值
    boolean lowMemory 如果当前可用内存<=threshold该值为真
  • Debug.MemoryInfo 统计进程下的内存信息
  • RunningAppProcessInfo 获取运行进程信息
    String processName 包名
    int pid 进程pid
    int uid 进程uid
    String[] pkgList 该进程下的所有程序的包名
  • RunningServiceInfo 封装了运行服务的信息
    activeSince 第一次被激活的时间
    foreground 服务是否在后台运行
  • ActivityManager.RunningTaskInfo

示例

统计进程信息

private List getRunningProcessInfo() {
    mAmProcessInfoList = new ArrayList();

    List appProcessList = mActivityManager.getRunningAppProcesses();
    
    for (int i = 0; i < appProcessList.size(); i++) {
        ActivityManager.RunningAppProcessInfo info = appProcessList.get(i);
        int pid = info.pid;
        int uid = info.uid;
        String processName = info.processName;
        int[] memoryPid = new int[]{pid};
        Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(memoryPid);   //统计进程下的内存信息
        int memorySize = memoryInfo[0].getTotalPss();

        AMProcessInfo processInfo = new AMProcessInfo();
        processInfo.setPid("" + pid);
        processInfo.setUid("" + uid);
        processInfo.setMemorySize("" + memorySize);
        processInfo.setProcessName(processName);
        mAmProcessInfoList.add(processInfo);
    }
    return mAmProcessInfoList;
}

public class AMProcessInfo {
    private String pid;
    private String uid;
    private String memorySize;
    private String processName;

    public AMProcessInfo() {
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getMemorySize() {
        return memorySize;
    }

    public void setMemorySize(String memorySize) {
        this.memorySize = memorySize;
    }

    public String getProcessName() {
        return processName;
    }

    public void setProcessName(String processName) {
        this.processName = processName;
    }
}

获得正在前端运行的activity,判断应用是否在前端运行

public static String getTopActivityName(Context context) {
    String topActivityClassName = null;
    ActivityManager activityManager = (ActivityManager) (context.getSystemService(Context.ACTIVITY_SERVICE));
    List runningTaskInfos = activityManager.getRunningTasks(1);
    if (runningTaskInfos != null) {
        ComponentName f = runningTaskInfos.get(0).topActivity;
        topActivityClassName = f.getClassName();
    }
    return topActivityClassName;
}

//判断应用是否在前端运行
public static boolean isRunningForeground(Context context) {
    String packageName = context.getPackageName();
    String topActivityClassName = getTopActivityName(context);
    if (packageName != null && topActivityClassName != null && topActivityClassName.startsWith(packageName)) {
        return true;
    } else {
        return false;
    }
}

你可能感兴趣的:(ActivityManager)