システムが実行するすべてのプロセスの情報を取得

8395 ワード

自分で書いたツールクラス
 
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

                + "]";

    }

}



}