reference : http://stackoverflow.com/questions/24590533/how-to-get-recent-tasks-on-android-l/26885469#26885469
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
String topPackageName ; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService("usagestats"); long time = System.currentTimeMillis(); // We get usage stats for the last 10 seconds List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time); // Sort the stats by the last time used if(stats != null) { SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>(); for (UsageStats usageStats : stats) { mySortedMap.put(usageStats.getLastTimeUsed(),usageStats); } if(mySortedMap != null && !mySortedMap.isEmpty()) { topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); } } }