title: Android M: How can I get the current foreground activity package name(from a service)
body: <p>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 more, the return list only contains my app task. Is there any way to settle it?</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>List&lt;ActivityManager.RunningAppProcessInfo&gt; runningAppProcessInfos = activityManager.getRunningAppProcesses();
  for (int i = 0; i &lt; runningAppProcessInfos.size(); i++) {
      ActivityManager.RunningAppProcessInfo runningAppProcessInfo = runningAppProcessInfos.get(i);
      if (runningAppProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
          return runningAppProcessInfo.pkgList[0];
      }
  }
  </code></pre>