get running task , process and service

public class MyActivityManager extends ExpandableListActivity {
    private static final String NAME = "NAME";
    private static final String NO_USED = "NO_USED";
    private String[] groups = { "showRecentTask", "showRunningTasks",
            "showRunningAppProcesses", "showRunningServices" };
    private ExpandableListAdapter mAdapter;

    ActivityManager mAm;
    PackageManager mPM;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAm = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        mPM = this.getPackageManager();

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

        for (int i = 0; i < groups.length; i++) {
            if (i == 0) {
                showRecentTask();
            } else if (i == 1) {
                showRunningTasks();
            } else if (i == 2) {
                showRunningAppProcesses();
            } else {
                showRunningServices();
            }

            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, groups[i] + "(" + strChildren.size() + ")");
            curGroupMap.put(NO_USED, "");

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < strChildren.size(); j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(NAME, strChildren.get(j));
                curChildMap.put(NO_USED, "");
            }
            childData.add(children);
        }

        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(this, groupData,
                android.R.layout.simple_expandable_list_item_1, new String[] {
                        NAME, NO_USED }, new int[] { android.R.id.text1,
                        android.R.id.text2 }, childData,
                android.R.layout.simple_list_item_1, new String[] { NAME,
                        NO_USED }, new int[] { android.R.id.text1,
                        android.R.id.text2 });
        setListAdapter(mAdapter);
    }

    public List<String> strChildren = new ArrayList<String>();

    // 最近开的task,HOME键长按会看到这个
    public void showRecentTask() {
        List<RecentTaskInfo> taskList = mAm.getRecentTasks(100, 0);
        strChildren.clear();
        for (RecentTaskInfo rti : taskList) {
            ResolveInfo ri = mPM.resolveActivity(rti.baseIntent, 0);
            if (ri != null) {
                Log.e("showRecentTask", "" + ri.loadLabel(mPM));
                strChildren.add(ri.loadLabel(mPM).toString());
            }
        }
    }

    // 运行中的任务
    public void showRunningTasks() {
        List<RunningTaskInfo> taskList = mAm.getRunningTasks(100);
        strChildren.clear();
        for (RunningTaskInfo rti : taskList) {
            Log.e("showRunningTasks", "Running task, numActivities="
                    + rti.numActivities);
            Log.e("showRunningTasks", ", description=" + rti.description);
            Log.e("showRunningTasks",
                    ", baseActivity=" + rti.baseActivity.getClassName());
            Log.e("showRunningTasks",
                    ", topActivity=" + rti.topActivity.getClassName());
            strChildren.add(rti.baseActivity.getClassName());
        }
    }

    // 运行中的作为app容器的process。
    public void showRunningAppProcesses() {
        List<RunningAppProcessInfo> processList = mAm.getRunningAppProcesses();
        strChildren.clear();
        for (RunningAppProcessInfo rapi : processList) {
            Log.e("showRunningAppProcesses", "" + rapi.processName);
            strChildren.add(rapi.processName);
        }
    }

    // 运行中的后台服务
    public void showRunningServices() {
        List<RunningServiceInfo> rsiList = mAm.getRunningServices(100);
        strChildren.clear();
        for (RunningServiceInfo rsi : rsiList) {
            Log.e("showRunningServices", "" + rsi.process);
            strChildren.add(rsi.process);
        }
    }
}

 

posted @ 2014-11-27 15:35  狐狸已化妖  阅读(625)  评论(0编辑  收藏  举报