• 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);
            }
        }
    }
  • 相关阅读:
    网络安全法课程上线喽~
    网络安全法将在六一正式实施,我该如何继续爱你?
    安卓逆向入门(一)
    如何正确的使用Ubuntu以及安装常用的渗透工具集.
    Linux/CentOS各种服务框架的搭建完整流程
    【Anroid界面实现】WindowManager类使用具体解释——用户首次打开APP的使用教学蒙板效果实现
    2014年辛星jquery解读第三节 Ajax
    迅为4412开发板Linux驱动教程/硬件知识及原理图的使用
    点评国内各大在线app生成平台
    OpenGL ES2.0 基本编程
  • 原文地址:https://www.cnblogs.com/larack/p/4126486.html
Copyright © 2020-2023  润新知