首页 > 代码库 > get running task , process and service
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 = http://www.mamicode.com/new ArrayList<Map<String, String>>(); List<List<Map<String, String>>> childData = http://www.mamicode.com/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); } }}
get running task , process and service
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。