zoukankan      html  css  js  c++  java
  • ANDROID获取Task及Process

    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);
            }
        }
    }
  • 相关阅读:
    选择器 nth-child和 nth-of-type的区别
    Numpy基础数据结构 python
    猜数字问题 python
    猴子吃桃问题 python
    random模块 time模块的用法 python
    统计输入任意的字符中中英文字母,空格和其他字符的个数 python
    匿名函数lambda python
    函数的可变参数问题
    两组列表组合成一个字典,一一对应
    遍历字典的几种方式
  • 原文地址:https://www.cnblogs.com/cnhome/p/2971649.html
Copyright © 2011-2022 走看看