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

3

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:

https://static.mcmap.net/file/mcmap/ZG-Ab5ovKRkQZV0nc79QWRft/1zpXg.jpg

Buncombe answered 17/12, 2011 at 3:24 Comment(3)
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.Oswald
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. :DBuncombe
You could try a ListView, or for the checkboxes, there's a tutorial here.Oswald
C
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;
}
Catabasis answered 17/12, 2011 at 7:40 Comment(6)
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. :)Buncombe
I'd like to ask what does the Log.d do?Buncombe
For Log window (Log.d) see this: https://mcmap.net/q/357514/-log-in-android-developmentCatabasis
It completely works now with the rest of my code. Thank you so much. :DBuncombe
This won't work if an app is using a non-standard process name.Shahaptian
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]Fornix
T
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 "";
}
Totality answered 18/6, 2012 at 22:31 Comment(2)
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.processNameRenounce
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/…Masha
C
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;
}
Catabasis answered 17/12, 2011 at 7:40 Comment(6)
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. :)Buncombe
I'd like to ask what does the Log.d do?Buncombe
For Log window (Log.d) see this: https://mcmap.net/q/357514/-log-in-android-developmentCatabasis
It completely works now with the rest of my code. Thank you so much. :DBuncombe
This won't work if an app is using a non-standard process name.Shahaptian
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]Fornix
W
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.

Whiteeye answered 17/12, 2011 at 6:7 Comment(3)
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.Buncombe
getting grep not found in the terminalZerk
My android console does have grep, Android 4.4 CyanogenModUnpolled

© 2022 - 2024 — McMap. All rights reserved.