Searching through stackoverflow i found exactly the same question i want to make:
What I am trying to achieve is to get the active 'window' title, for example, when I open a web browser the title is "www.google.com" and if I open a conversation with Mister X in SMS/MMS, the current title is "Mister X 0245478767".
There wasn't any concrete answer. One answer suggested to use getTitle() method from Activity class, but doesn't explain how to use it through activitymanager.
My code to get the current active App is this:
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo.topActivity.getPackageName();
PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
foregroundTaskAppNameOld = foregroundTaskAppName;
foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();