title: Android 6.0 Marshmallow UsageStatsManager issue when trying to retrieve foreground app
body: <p>whenever I try to query the Usage Stats of the UsageStatsManager I can correctly get the last running app. However, if I pull down the status bar and there is a new notification, the last used app (based on UsageStats) will change to that of the notification. As a result, I get false alarms that the foreground application has changed.</p>
  
  <p>I can't seem to find a way to filter those specific draws. Any ideas?</p>
  
  <p>Right now, I query for the foreground app with the following code. The problem exists only in Marshmallow (5.X works correctly).</p>
  
  <pre><code>UsageStatsManager mUsageStatsManager = (UsageStatsManager) rotationManager.getSystemService("usagestats");
  long time = System.currentTimeMillis();
  // We get usage stats for the last 10 seconds
  List&lt;UsageStats&gt; stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 2, time);
  // Sort the stats by the last time used
  if (stats != null) {
  SortedMap&lt;Long, UsageStats&gt; mySortedMap = new TreeMap&lt;Long, UsageStats&gt;();
  for (UsageStats usageStats : stats) {
      mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
  }
  if (mySortedMap != null &amp;&amp; !mySortedMap.isEmpty()) {
      foregroundApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
  } 
  }
  </code></pre>