ANDROID擷取Task及Process

來源:互聯網
上載者:User
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);        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.