0

I've seen many posts about how to get the running processes on Android, but things changed with Lolipop. Now the list is trunked for non rooted devices.

Every post was about to do this kind of code:

ActivityManager am = (ActivityManager) mContext
            .getSystemService(Activity.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = this.getActivityManager().getRunningAppProcesses();
    List<ActivityManager.RunningTaskInfo> taskInfo = this.getActivityManager().getRunningTasks(Integer.MAX_VALUE);

I tried it and the only thing that is retrieved is your current application.

@Stan was talking about developping an accessibility service here in this post: How to check current running applications in Android?

I checked the API, and what I saw is that you could be informed of application launch with an accessibility service, but not if it's still running afterward. So I don't think this is the good way to do it.

Does anyone have an idea about how to do it?

Community
  • 1
  • 1
kobe1980
  • 13
  • 6

1 Answers1

0

In Android Lolipop your code will not work as from Android L onward getRunningTask will not work. You have to use getAppRunningProcess.

Here is the code:-

public static String[] getActivePackagesCompat() {
  final List<ActivityManager.RunningTaskInfo> taskInfo = mActivityManager.getRunningTasks(1);
  final ComponentName componentName = taskInfo.get(0).topActivity;
  final String[] activePackages = new String[1];
  activePackages[0] = componentName.getPackageName();
  return activePackages;
}

public static String[] getActivePackages() {
  final Set<String> activePackages = new HashSet<String>();
  final List<ActivityManager.RunningAppProcessInfo> processInfos = mActivityManager.getRunningAppProcesses();
  for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
        activePackages.addAll(Arrays.asList(processInfo.pkgList));
      } 
   }
   return activePackages.toArray(new String[activePackages.size()]);
}
  • Hi, that's exactly what I started to do, but the only app retrieve by getRunningAppProcesses is my own application. Whatever I do on the phone. And AppCompat return my app or com.google.android.googlequicksearchbox. Not sure to understand. – kobe1980 Apr 27 '16 at 22:16
  • You should follow this Link:-http://stackoverflow.com/questions/26400469/alternative-to-getrunningtasks-in-android-l – Praween Kumar Mishra Apr 28 '16 at 06:09
  • Thanks @Praween. That's exactly what i was looking for :) – kobe1980 Apr 28 '16 at 10:41