You may follow the steps:
- Retrieve the list of the tasks that are currently running by calling
getRunningTasks()
- Go through each of the task and retrieve the
PackageName
- Get
AppName
from the PackagName
- Add the
AppName
in a list
Here is the implementation:
private List<String> getRunningAppList()
{
Set<String> appNameList = new HashSet<>();
PackageManager pm = getPackageManager();
ActivityManager aManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// Get the list of the tasks that are currently running
List<ActivityManager.RunningTaskInfo> infoList = aManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo taskInfo : infoList)
{
// Get the packageNAme
String pkgName = taskInfo.topActivity.getPackageName();
String appName = null;
// Get the appName
try
{
appName = pm.getApplicationLabel(pm.getApplicationInfo(pkgName,PackageManager.GET_META_DATA)).toString();
} catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
// Adding the appName
appNameList.add(appName);
}
return new ArrayList<>(appNameList);
}