15

I am trying to make a program that shows all the active applications.

I searched everywhere but could only find code that shows the package name only.

It would be of great help if you masters can tell me how to display all the active application name

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Shijilal
  • 2,175
  • 10
  • 32
  • 55

3 Answers3

34

Did you try using ActivityManager.getRunningAppProcesses()? Here is the sample code for retrieving names:

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
  try {
    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
    Log.w("LABEL", c.toString());
  }catch(Exception e) {
    //Name Not FOund Exception
  }
}
bhups
  • 14,345
  • 8
  • 49
  • 57
  • yes..i did tried..but in ActivityManager.getRunningAppProcess() there is only option for getting ProcessName. But i need the ApplicationName of those Process and there is no way i could find in that. – Shijilal Oct 05 '10 at 06:48
  • @bhups can u pls tell mw how can i find only system running apps not installed apps ? – user3233280 Nov 23 '14 at 07:38
  • Hi @bhups, I am currently working an app that when a button is clicked, it will check if the app is active(e.g. Facebook) then terminate the process or app. Is that possible? – Jeongbebs Jan 07 '15 at 03:56
1

If you are getting the package name, you should be able to get additional information about the application using the PackageManager:

http://developer.android.com/reference/android/content/pm/PackageManager.html

There are direct methods for getting the application icon, ApplicationInfo and ActivityInfo objects. Off the top of my head I don't know which one would direct you to the readable name, but if its not directly accessible through one of the methods here, it should be accessible from the application resources (also accessible from this class).

Charles B
  • 1,533
  • 8
  • 14
-2

If you are writing a service and want to update the current "foreground app" at regular intervals like I did, DO NOT be tempted to get the ActivityManager instance in the onCreate() of your service and re-use it when updating the current app name:

public class MyService extends Service implements {
  ActivityManager mActivityManager;
    
  @Override   public void onCreate()   {
        mActivityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE );   }


  String getForegroundAppName() {
    String appname;

    List <RunningAppProcessInfo> l;
    l = mActivityManager.getRunningAppProcesses();
    
    while( i.hasNext() ) {
      if ( info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && !isRunningService(info.processName) {
        currentApp = info;
        break;
      }
    }

    if ( currentApp != null ) {
      try {
        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(currentApp.processName, PackageManager.GET_META_DATA ));
        appname = c.toString();
    }
    return appname;
  }

}

Don't do this. It causes a memory leak resulting in numerous GC_CONCURRENT errors every time it's called. I don't know the real reason behind this but it's much cleaner to get the ApplicationManager instance each time you use it like this:

public class MyService extends Service implements {
        
      @Override   public void onCreate()   {...   }
    
    
      String getForegroundAppName() {
        ActivityManager mActivityManager;
        String appname;
    
        mActivityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE );
        List <RunningAppProcessInfo> l;
        l = mActivityManager.getRunningAppProcesses();
        
        while( i.hasNext() ) {
          if ( info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && !isRunningService(info.processName) {
            currentApp = info;
            break;
          }
        }
    
        if ( currentApp != null ) {
          try {
            CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(currentApp.processName, PackageManager.GET_META_DATA ));
            appname = c.toString();
        }
        return appname;
      }
    
    }
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Zoccadoum
  • 862
  • 1
  • 10
  • 16