Android - How to get the processName or packageName by using PID?

47,329

Solution 1

Hello you can use this code, it works for me in Android 2.3.3:

private String getAppName(int pID)
{
    String processName = "";
    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 
          { 
              if(info.pid == pID)
              {
                  CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                  //Log.d("Process", "Id: "+ info.pid +" ProcessName: "+ info.processName +"  Label: "+c.toString());
                  //processName = c.toString();
                  processName = info.processName;
              }
          }
          catch(Exception e) 
          {
                //Log.d("Process", "Error>> :"+ e.toString());
          }
   }
    return processName;
}

Solution 2

This code is a simplified version of Yaqub's code. I use this as a static method in a Util class:

public static String getAppNameByPID(Context context, int pid){
    ActivityManager manager 
               = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for(RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()){
        if(processInfo.pid == pid){
            return processInfo.processName;
        }
    }
    return "";
}

Solution 3

Kill other processes is generally bad idea..

Look at this Question Android process killer and android task kill..

And also this blog Android: Killing a running process with processid(pid) and package name

And for your question How to get Process Name from pid then

Something like,

Install a terminal emulator, launch it and run:

ps | grep 10058

ps lists the processes and grep filters for the ID you want.

But this only works if the application is running when you run the command.

Share:
47,329

Related videos on Youtube

Loren
Author by

Loren

More than three years experience in the financial software industry. Involved in the development and maintenance of payment processing applications.

Updated on July 09, 2022

Comments

  • Loren
    Loren almost 2 years

    MY QUESTION: What could I use to retrieve the processName or packageName of a certain process given its PID?

    Since in my task manager I wanted to use the PID while utilizing the killBackgroundProcesses code to kill the processes. Problem is I need the packageName/processName to do that and it would be such a hassle to the user if I asked them to type in the processName rather than just typing its PID.

    here's the image of my task manager:

    http://i.imgur.com/1zpXg.jpg

    • Rob I
      Rob I
      Not what you asked, but perhaps in the final version of your task manager you'd let the user click to select which app to kill, or use checkboxes. Having them type the PID in seems like an extra, unnecessary step.
    • Rob I
      Rob I
      You could try a ListView, or for the checkboxes, there's a tutorial here.
  • Loren
    Loren over 12 years
    Thanks for the code. I'm just trying to figure it out first since I'm not that well-versed in Android and it's been a long time since I programmed anything. :)
  • Loren
    Loren over 12 years
    Yup, I've read that several times. However, it's a project in our class so I have no choice but to comply with the requirements. Thanks for helping.
  • Loren
    Loren over 12 years
    I'd like to ask what does the Log.d do?
  • Yaqub Ahmad
    Yaqub Ahmad over 12 years
  • Loren
    Loren over 12 years
    It completely works now with the rest of my code. Thank you so much. :D
  • Gabor
    Gabor about 12 years
    getting grep not found in the terminal
  • Felix
    Felix over 10 years
    This won't work if an app is using a non-standard process name.
  • dazito
    dazito almost 10 years
    My android console does have grep, Android 4.4 CyanogenMod
  • Mike Venzke
    Mike Venzke over 9 years
    If your process is a remote service, this won't work. You'll want to do the same thing, but call am.getRunningServices(), and use an ActivityManager.RunningServiceInfo object. For me, info.process then has: com.[package name]:[process name]
  • Naveed
    Naveed almost 9 years
    Sorry I din't get your point The pid that we are passing to this function we are already getting from RunningAppProcessInfo object so instead of passing to this function and again get list of all running apps, we can directly call property from RunningAppProcessInfo object i,e processInfo.processName
  • Amaksoft
    Amaksoft over 5 years
    Doesn't work on API above 23 as getRunningAppProcesses() only returns your application's processes. Apparently there is no correct way to obtain other application package by its PID. See Dianne Hackborn answer to similar question. groups.google.com/d/msg/android-platform/pmL5wl2w7PU/…

Related