You can do this by using ActivityManager.RunningTaskInfo
ActivityManager has method getrunningtasks() and using that try to find a solution to your problem Activity manager seems to be the solution you are searching for.
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < tasks.size(); i++) {
Log.d("Running task", "Running task: " + tasks.get(i).baseActivity.toShortString() + "\t\t ID: " + tasks.get(i).id);
}
also have a look at following thread See Android recent task executed by the user
note:
The method getRunningTasks(int maxNum), was deprecated in the API level 21(LOLLIPOP), for security reasons, the possibility of leaking personal information to third-party applications. (If you use this method to be aware that it will not be supported for future Android versions).