How to check current running applications in Android?
Asked Answered
T

3

33

I want to check current running applications in android programmatically, similar to how it shows 6 applications if we press and hold the HOME button.

I'm mostly interested in the application names.

Toot answered 19/7, 2010 at 6:33 Comment(0)
S
52

You can get current name package using

ActivityManager am = (ActivityManager) mContext
                .getSystemService(Activity.ACTIVITY_SERVICE);
        String packageName = am.getRunningTasks(1).get(0).topActivity
                .getPackageName();

You can use this package name to get current active application

Strobe answered 26/3, 2011 at 16:16 Comment(6)
Awesome answer. I was looking for this forever! This gives the task that the user is currently doing (program they are currently in). Great!Fashionable
Requires permission: android.permission.GET_TASKSSwell
The getRunningTasks method has been truncated in the latest API for Android Lollipop. Do you have any suggestions on an alternative way to do this?Gentleman
@prajitdas, the only workaround on a nonrooted device is to develop an accessiblity service (look here and here), which must be explicitly enabled by device's user.Fixation
BTW, on Android 5 you can even get the list of 0 size as a result of getRunningTasks, so get(0) raises the "IndexOutOfBoundsException: Invalid index 0, size is 0". This information comes from Google Play ANR reporting tool for a published application.Fixation
Out of curiosity, how can Fleksy keyboard do this without requiring an accessibility service to be enabled?Blandishments
G
16

You can check the processName of each element in the list to see if it's the process you're looking for. You can use this code

boolean isNamedProcessRunning(String processName){
 if (processName == null) 
  return false;

 ActivityManager manager = 
    (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
 List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
 for (RunningAppProcessInfo process : processes)
 {
    if (processName.equals(process.processName))
    {
        return true;
    }
 }
 return false;
}
Grannia answered 27/4, 2011 at 9:11 Comment(0)
S
11

A long press of the HOME key brings up a list of recent (not necessarily running) tasks.

ActivityManager has what you need. Look at getRunningAppProcesses or getRunningTasks.

You can view this information (and much more) by running dumpsys activity via 'adb shell'.

  Running processes (most recent first):
    App  #11: adj=fore /F 43fe7c20 233:com.android.browser/10004 (top-activity)
    App  #10: adj=bak  /B 43dcec80 190:android.process.media/10009 (bg-empty)
    App  # 9: adj=vis  /F 43f495c8 107:com.android.inputmethod.latin/10014 (service)
              com.android.inputmethod.latin.LatinIME<=ProcessRecord{43dbe0e8 59:system/1000}
    PERS # 8: adj=sys  /F 43dbe0e8 59:system/1000 (fixed)
    PERS # 7: adj=core /F 43f534c0 111:com.android.phone/1001 (fixed)
    App  # 6: adj=bak+1/B 43ea1f58 148:android.process.acore/10006 (bg-empty)
    App  # 5: adj=home /B 43f601c0 114:com.android.launcher/10000 (home)
    App  # 4: adj=bak+2/B 43f85128 133:com.android.settings/1000 (bg-empty)
    App  # 3: adj=bak+3/B 43eacae0 223:com.android.music/10029 (bg-empty)
    App  # 2: adj=bak+4/B 43dfc500 206:com.android.mms/10028 (bg-empty)
    App  # 1: adj=bak+5/B 43f8fcd0 166:com.android.alarmclock/10025 (bg-empty)
    App  # 0: adj=bak+6/B 43fcbe50 182:com.android.email/10008 (bg-empty)

  PID mappings:
    PID #59: ProcessRecord{43dbe0e8 59:system/1000}
    PID #107: ProcessRecord{43f495c8 107:com.android.inputmethod.latin/10014}
    PID #111: ProcessRecord{43f534c0 111:com.android.phone/1001}
    PID #114: ProcessRecord{43f601c0 114:com.android.launcher/10000}
    PID #133: ProcessRecord{43f85128 133:com.android.settings/1000}
    PID #148: ProcessRecord{43ea1f58 148:android.process.acore/10006}
    PID #166: ProcessRecord{43f8fcd0 166:com.android.alarmclock/10025}
    PID #182: ProcessRecord{43fcbe50 182:com.android.email/10008}
    PID #190: ProcessRecord{43dcec80 190:android.process.media/10009}
    PID #206: ProcessRecord{43dfc500 206:com.android.mms/10028}
    PID #223: ProcessRecord{43eacae0 223:com.android.music/10029}
    PID #233: ProcessRecord{43fe7c20 233:com.android.browser/10004}
Scow answered 19/7, 2010 at 7:56 Comment(1)
i am talking about pro grammatically.Toot

© 2022 - 2024 — McMap. All rights reserved.