在Android中获取活动的标题

时间:2015-07-29 20:34:15

标签: android android-package-managers android-applicationinfo

我正在寻找一种在Android Lollipop中检索活动标题的方法。

我的意思不是流程名称,甚至是应用程序的名称,而是活动打开时给予的标题。例如,当您在Chrome中导航至http://www.google.com/时,标题会从“Chrome”更改为“Google”,当您点击最近的应用按钮时可以看到该标题,我需要一些东西返回“Google”一词。

以下是我在循环浏览当前正在运行的应用程序并尝试查找该信息的可怕尝试。

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

    for (int i = 0; i < runningAppProcessInfo.size(); i++) {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo info = null;
        try {
            info = pm.getApplicationInfo(runningAppProcessInfo.get(i).processName, 0);
            Log.i("DEBUG_LABEL", "Process Name: " + runningAppProcessInfo.get(i).processName);
            Log.i("DEBUG_LABEL", "App Label: " + pm.getApplicationLabel(info).toString());
            if (info.className != null) {
                Log.i("DEBUG_LABEL", "Info's package and class name: " + info.packageName + ", " + info.className);
                ComponentName cName = new ComponentName(info.packageName, info.className);
                ActivityInfo activityInfo = pm.getActivityInfo(cName, PackageManager.GET_META_DATA);
                Log.i("DEBUG_LABEL", "Activity Label: " + activityInfo.loadLabel(pm));
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:1)

显然我正在寻找的是任务的description

List<ActivityManager.RunningTaskInfo> rTaskInfoList = am.getRunningTasks(100);

...

return rTaskInfoList.get(i).description.toString() // Returns "Google", instead of "Chrome"