Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 722
Loren Avatar asked Dec 17 '11 03:12

Loren


1 Answers

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 "";
}
like image 119
Jeremy C Avatar answered Oct 25 '22 18:10

Jeremy C