0

I'm looking for a way to retrieve an activity's title in Android Lollipop.

What I mean is not the process name or even the app's name, but the title that is given to the activity when it is open. For example, when you navigate to http://www.google.com/ in Chrome, the title changes from "Chrome" to "Google", which can be seen when you hit the recent apps button, and I need something that returns the word "Google".

Below is my awful attempt at looping through the currently running apps and trying to find that information.

    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();
        }
    }
Abe Fehr
  • 729
  • 9
  • 23
  • Is this your app or ALL apps? – VM4 Jul 29 '15 at 20:37
  • This is supposed to work for all apps(ideally) – Abe Fehr Jul 29 '15 at 20:39
  • http://stackoverflow.com/questions/11391451/list-out-installed-running-applications-in-android-programmatically – VM4 Jul 29 '15 at 20:40
  • That's not overly helpful. The second method in the link is similar to what I was doing, so I just tried the first method with `queryIntentActivities()` and while I got the Activity, calling `loadLabel()` just gave me "Chrome". I'd like sure-fire way to get the *same* title that's being shown in the recent apps screen – Abe Fehr Jul 29 '15 at 20:52

1 Answers1

1

Apparently what I was looking for was the description of the Task.

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

...

return rTaskInfoList.get(i).description.toString() // Returns "Google", instead of "Chrome"
Abe Fehr
  • 729
  • 9
  • 23