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();
}
}