Android - 如何使用 PID 获取 processName 或 packageName? [英] Android - How to get the processName or packageName by using PID?
问题描述
我的问题:给定某个进程的 PID
,我可以用什么来检索它的 processName
或 packageName
?
MY QUESTION: What could I use to retrieve the processName
or packageName
of a certain process given its PID
?
因为在我的任务管理器中,我想在利用 killBackgroundProcesses 代码杀死进程的同时使用 PID.问题是我需要 packageName/processName 来做到这一点,如果我让他们输入 processName 而不是仅仅输入它的 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:
推荐答案
您好,您可以使用此代码,它在 Android 2.3.3 中适用于我:
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;
}
这篇关于Android - 如何使用 PID 获取 processName 或 packageName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!