Get active Application name in Android
Asked Answered
B

3

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

Brancusi answered 5/10, 2010 at 5:39 Comment(0)
T
35

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
  }
}
Trilby answered 5/10, 2010 at 6:23 Comment(3)
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.Brancusi
@Trilby can u pls tell mw how can i find only system running apps not installed apps ?Edisonedit
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?Goodfellowship
C
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).

Confessional answered 5/10, 2010 at 7:5 Comment(0)
C
-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;
      }
    
    }
Commutation answered 24/4, 2012 at 15:20 Comment(1)
Deleting can be done with the delete link on your post. Editing out your answer is not the way to do it.Fearnought

© 2022 - 2024 — McMap. All rights reserved.