I'm new to Android and i'm trying to make a project that allows user to see which applications are running. I used this code to get the packagename of the running task on top:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { List<ActivityManager.RunningAppProcessInfo> runningappInfo = activityManager.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo app : runningappInfo) { if (app.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && app.importanceReasonCode == 0) { Integer state = null; try { state = field.getInt(app); } catch (IllegalAccessException e) { e.printStackTrace(); } if (state != null && state == PROCESS_STATE_TOP) { currentInfo = app; break; } } } if (currentInfo != null) { current = currentInfo.processName.split(":")[0]; } }else{ List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1); ComponentName componentInfo = runningTaskInfos.get(0).topActivity; current = componentInfo.getPackageName(); }
current
here is packagename of the runningtask. But this code didn't work any more after Google update the new android version Lollipop5.1.1. The method "getRunningAppProcesses" now returns a list null.
I get stuck now because all other functions in my project work only if I can get the runningtask's packagename. If anybody has resolved this kind of problem, Could you please help me?
Thanks a lot,