zoukankan      html  css  js  c++  java
  • 获取系统运行的所有进程的信息

    自己写的一些工具类

    public class TaskInfoUtils {
        /**
         * 获取全部进程的信息
         * 
         * @param pContext
         * @return
         */
        public static List<TaskInfo> getTaskInfos(Context pContext) {
            // 1获取到ActivityManager和PackageManager
            ActivityManager _ActivityManager = (ActivityManager) pContext
                    .getSystemService(pContext.ACTIVITY_SERVICE);
            PackageManager _PackageManager = pContext.getPackageManager();
            List<TaskInfo> _TaskInfos = new ArrayList<TaskInfo>();
            // 2获取到所有在运行的进程
            List<RunningAppProcessInfo> _RunningAppProcessInfos = _ActivityManager
                    .getRunningAppProcesses();
            //3 遍历进程,获取他的信息
            for (RunningAppProcessInfo runningAppProcessInfo : _RunningAppProcessInfos) {
                TaskInfo _TaskInfo = new TaskInfo();
                MemoryInfo[] memoryInfos = _ActivityManager
                        .getProcessMemoryInfo(new int[] { runningAppProcessInfo.pid });
                long _MemSize = memoryInfos[0].getTotalPrivateDirty() * 1024;// 进程消耗的内存
                String _packageName = runningAppProcessInfo.processName;// 进程的包
                ApplicationInfo _ApplicationInfo;
                try {
                    _ApplicationInfo = _PackageManager.getApplicationInfo(
                            _packageName, 0);
                    Drawable _Icon = _ApplicationInfo.loadIcon(_PackageManager);// 程序图标
                    String _Name = _ApplicationInfo.loadLabel(_PackageManager)
                            .toString();// 程序名
    
                    _TaskInfo.setIcon(_Icon);
                    _TaskInfo.setMemsize(_MemSize);
                    _TaskInfo.setName(_Name);
                    _TaskInfo.setPackname(_packageName);
                    if ((_ApplicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                        // 用户进程
                        _TaskInfo.setUserTask(true);
                    } else {
                        // 系统进程
                        _TaskInfo.setUserTask(false);
                    }
                    _TaskInfos.add(_TaskInfo);
                } catch (NameNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
            return _TaskInfos;
        }
     class TaskInfo {
        private Drawable icon;
        private String name;
        private long memsize;
        private boolean userTask;
        private String packname;
    
        public Drawable getIcon() {
            return icon;
        }
    
        public void setIcon(Drawable icon) {
            this.icon = icon;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public long getMemsize() {
            return memsize;
        }
    
        public void setMemsize(long memsize) {
            this.memsize = memsize;
        }
    
        public boolean isUserTask() {
            return userTask;
        }
    
        public void setUserTask(boolean userTask) {
            this.userTask = userTask;
        }
    
        public String getPackname() {
            return packname;
        }
    
        public void setPackname(String packname) {
            this.packname = packname;
        }
    
        @Override
        public String toString() {
            return "TaskInfo [ name=" + name + ", memsize="
                    + memsize + ", userTask=" + userTask + ", packname=" + packname
                    + "]";
        }
    }
    
    }
  • 相关阅读:
    JavaScript基础
    CSS——网页的布局方式
    CSS——选择器及三大特性
    CSS——常用属性
    Redis——Redis持久化机制、Jedis的使用、Jedis连接池
    Redis——NOSQL、Redis概述、Redis数据类型、通用命令
    JDBC——使用JDBC事务分层完成转账案例
    JDBC——JDBC中的事务、DBUtils使用事务
    Dao组件
    集合的介绍(完全版)
  • 原文地址:https://www.cnblogs.com/liuying3013/p/4166318.html
Copyright © 2011-2022 走看看