It is easy to get a list of running tasks from the ActivityManager service on Android L, and the current active task is returned first. But it don\'t work on Android M any m
Please try this lines in Android M. This worked for me
String packageName = ProcessManager.getRunningForegroundApps(getApplicationContext()).get(0).getPackageName();
check below link for other android platform support.
https://stackoverflow.com/a/36660429/4554069