5

I'm creating a home launcher and I want to have a compatibility with the Android 5.0, Lollipop. I want to get a list of recent apps on the launcher.

But since ActivityManager.getRecentTasks() no longer works in API 21, how can I do this ?

TLama
  • 75,147
  • 17
  • 214
  • 392
rosghub
  • 8,924
  • 4
  • 24
  • 37

2 Answers2

10
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();                                   
        }                                       
    }
}  

Using the UsageStatsManager, you can get the foreground package names currently running.

Source: How to get recent tasks on Android "L"?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • I actually tried this, but I'm confused on the query part. 10 seconds of query stats doesn't work, is there a ways to query since the devices booted? Also in the result map there are apps like system UI, lockscreen that show up and i'm not sure how to filter those out? – rosghub Dec 04 '14 at 01:26
  • `android.os.SystemClock.uptimeMillis()` returns milliseconds since boot :) – An SO User Dec 04 '14 at 01:30
  • Seems to work thank you! It still only gives me like 10 results when my phone has been on for days, seems like there should be more. But its fine for my purposes as I only need <8. – rosghub Dec 04 '14 at 04:59
  • 2
    Just FYI the getSystemService(Context.USAGE_STATS_SERVICE) is for level 22 and over only. There may be a gap in API 21 where you can't use getRecentTasks and you can't use the UsageStatsManager. (http://developer.android.com/reference/android/content/Context.html#USAGE_STATS_SERVICE) – Scott Jan 16 '16 at 01:43
  • What the String "usagestats" ? – user5716019 Feb 26 '16 at 06:21
  • replace the String "usagestats" to Context.USAGE_STATS_SERVICE – Aakanksha Jul 14 '16 at 13:38
  • this won't work unless the user give the permission to use the usage stats – ThunderWiring Sep 28 '17 at 10:19
  • @Scott it should be fine. UsageStatsManager [exists in 21](https://github.com/aosp-mirror/platform_frameworks_base/blob/lollipop-release/core/java/android/app/usage/UsageStatsManager.java), as well as the [USAGE_STATE_SERVICE](https://github.com/aosp-mirror/platform_frameworks_base/blob/lollipop-release/core/java/android/content/Context.java#L2873) constant. The latter is just hidden for some reason. – TheWanderer Aug 02 '18 at 22:57
  • How to know that app goes background? – Naxsami Dec 09 '20 at 06:00
  • `stats` size is always zero! even by changing `UsageStatsManager.INTERVAL_DAILY` and the timeframe – Dr.jacky Mar 02 '23 at 10:52
0

I was troubled with whis, since android has deprecated some api such as getRecentTasks,getRunningTasks above API21, they say these method is no longer available to third party app. When i call these api on the device above lollipop, it just return null or my app :(.

Then i found a method to instead of these api, as long as we can get the app'spackagename, we can do almost anything we want. Android based on linux, i found this AndroidProcesses.It can use /proc to get android's application pid and processName, and the processName is such ad com.xxx.xxx:remote. Notice that the front of : is the packageName :)

After get the packageName, i think you know what to do. I think we can start a Service to statistics the most recent used app according to the process running background. The longer the statistical time, the more accurate the results.

da que
  • 73
  • 1
  • 5