5

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?

My code:

List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfos.size(); i++) {
    ActivityManager.RunningAppProcessInfo runningAppProcessInfo = runningAppProcessInfos.get(i);
    if (runningAppProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
        return runningAppProcessInfo.pkgList[0];
    }
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Mr.Clear
  • 53
  • 1
  • 4

2 Answers2

13

you can use below code and get the current foreground activity package name.

   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            UsageStatsManager usm = (UsageStatsManager) getSystemService("usagestats");
            long time = System.currentTimeMillis();
            List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
                            time - 1000 * 1000, time);
                    if (appList != null && appList.size() > 0) {
                        SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
                        for (UsageStats usageStats : appList) {
                            mySortedMap.put(usageStats.getLastTimeUsed(),
                                    usageStats);
                        }
                        if (mySortedMap != null && !mySortedMap.isEmpty()) {
                            currentApp = mySortedMap.get(
                                    mySortedMap.lastKey()).getPackageName();
                        }
                    }
        } else {
                ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
                currentApp = am.getRunningTasks(1).get(0).topActivity .getPackageName(); 

        }

Edit

Add this permission in to Manifest file.

<uses-permission android:name="android.permission.GET_TASKS" /> 
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />

Note

Make sure you need to configure custom setting in your device to obtain the output you can config it with Setting > Security > Apps with usage access > Then enable your app permission

Vishal Butani
  • 356
  • 4
  • 12
  • Won't work on some devices, which do not have any order in the RunningAppProcessInfo list. Therefore tasts.get(0) will return always the false process. – JacksOnF1re Dec 10 '15 at 18:00
  • Try This code i hope it is useful to you ---- ' ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE); currentApp = am.getRunningTasks(1).get(0).topActivity .getPackageName(); ' @JacksOnF1re – Vishal Butani Dec 11 '15 at 08:58
  • But it is deprecated since API 21 :( The thing is, I can't use the UsageStats Manager, because samsung decided to not support it's functionality – JacksOnF1re Dec 11 '15 at 09:25
  • yes you are right this code deprecated since API 21 : but it properly work below the API 21, so first check the API version and add condition for different version.. i also use above code in my app, and it work successfully. thanks @JacksOnF1re – Vishal Butani Dec 11 '15 at 10:13
  • Thanks so far, but what I am supposed to do in API 21 then? As said, UsageStats does not work on every device. – JacksOnF1re Dec 11 '15 at 10:35
-1

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

Community
  • 1
  • 1
AmAnDroid
  • 246
  • 2
  • 11