I use the following code to get the activity name of the foreground app in the variable foregroundTaskPackageName. It works on all OS versions between 4.1 to 4.4, but does not work in Android 5.0 Lollipop.
Can anyone help with what has changed in 5.0 Lollipop? In Lollipop - the text I get for foregroundTaskPackageName is just 'Launcher3'. I am using the Genymotion Emulator.
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0); // get
// list
// of
// running
// tasks
String foregroundTaskAppName = null;
String foregroundTaskPackageName = foregroundTaskInfo.topActivity
.getPackageName();
This works for me on Lollipop (21):
you can use below code and get the current foreground activity package name.
Try this
You need to use the new
UsageStatsManager
and call itsqueryUsageStats
method to get the history of activities launched. Please note that the user will be required to provide access to usage stat on the device settings at Security->Apps with usage access.Links:
UsageStatsManager
documentationqueryUsageStats
method documentationan available but not best way is to use accessibility.
Declare an accessibility service in AndroidManifest xml file
accessibility_service_config.xml file
store the activity name when window state changed
The disadvantage is that you need to let users enable your accessibility service in Settings.
i have created a class that uses
/system/bin/toolbox
command to identify processes and then identifies visible application. Need to add identifies system apps with no UI and android launchers.ProcessManager.java