18

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

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
Loren
  • 1,260
  • 5
  • 16
  • 23
  • 1
    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 Dec 17 '11 at 09:14
  • Yup, that's my ideal plan. However, I don't know how to use the checkboxes or click to select method yet. That's why I opted to do this method first. But if you've got an idea on how to do it, it would really help me a lot. :D – Loren Dec 17 '11 at 09:26
  • 1
    You could try a [ListView](http://developer.android.com/reference/android/widget/ListView.html), or for the checkboxes, there's a tutorial [here](http://www.anddev.org/checkbox_text_list___extension_of_iconified_text_tutorial-t771.html). – Rob I Dec 18 '11 at 00:49

3 Answers3

21

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 "";
}
Rob Kielty
  • 7,958
  • 8
  • 39
  • 51
Jeremy C
  • 609
  • 5
  • 10
  • 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 – Naveed May 28 '15 at 04:48
  • 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. https://groups.google.com/d/msg/android-platform/pmL5wl2w7PU/8r4SSQ6jKVkJ – Amaksoft Oct 12 '18 at 14:11
14

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;
}
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • 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 Dec 17 '11 at 08:55
  • I'd like to ask what does the Log.d do? – Loren Dec 17 '11 at 09:21
  • For Log window (Log.d) see this: http://stackoverflow.com/questions/4584103/log-in-android-development – Yaqub Ahmad Dec 17 '11 at 10:05
  • It completely works now with the rest of my code. Thank you so much. :D – Loren Dec 17 '11 at 11:12
  • This won't work if an app is using a non-standard process name. – Felix Sep 20 '13 at 12:45
  • 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] – Mike Venzke Aug 20 '14 at 18:17
2

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.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151