Android的 - 如何让processName或通过的packageName采用PID? [英] Android - How to get the processName or packageName by using PID?
问题描述
我的问题:我能使用它来检索的 processName
或的packageName
给予一定的过程的 PID
?
MY QUESTION: What could I use to retrieve the processName
or packageName
of a certain process given its PID
?
由于我的任务管理器,我想使用PID同时利用killBackgroundProcesses code杀掉的进程。问题是我需要的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:
推荐答案
您好:您可以使用此code,它为我的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的 - 如何让processName或通过的packageName采用PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!