如何以编程方式获取有关android中所有正在运行的活动的信息?

android中,通过观察Logcat获取有关运行活动的信息.

例如,如果您打开相机,它将记录在Logcat中.

有没有办法以编程方式获取该信息?

最佳答案
ActivityManager m = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE );
    List<RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(10);
    Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
    while(itr.hasNext())
    {
        RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
        int id = runningTaskInfo.id;
        CharSequence desc= runningTaskInfo.description;
        String topActivity = runningTaskInfo.topActivity.getShortClassName();
        int numOfActivities = runningTaskInfo.numActivities;
    }

注意:您必须在Manifest文件中指定android.permission.GET_TASKS权限.

转载注明原文:如何以编程方式获取有关android中所有正在运行的活动的信息? - 代码日志