您的位置:首页 > 其它

根据进程ID获取进程名字

2017-03-01 15:34 363 查看
private static String processName() {
//      shell获取指定pid的进程的名字
//      ps | grep pid | awk '{print $9}'
String processName=null;
try {
int pid = android.os.Process.myPid();///获取进程的PID
String command="ps | grep "+pid+" | awk '{print $9}'";
String res=ShellExe.run(command, 20);
if (res!=null&&res.length()>0&&!res.contains("not found")&&
!res.contains("nvalid") && !res.contains("Read-only") &&
!res.contains("Usage: mount")){
processName=res;
}else{
command="ps | busybox grep "+pid+" |busybox awk '{print $9}'";
res=ShellExe.run(command, 20);
if (res!=null&&res.length()>0&&!res.contains("not found")&&
!res.contains("nvalid") && !res.contains("Read-only") &&
!res.contains("Usage: mount")){
processName=res;
}else{
////////使用ps命令过滤出pid的进程信息
command="ps";
res=ShellExe.run(command, 20);
if (res!=null&&res.length()>0&&!res.contains("not found")&&
!res.contains("nvalid") && !res.contains("Read-only") &&
!res.contains("Usage: mount")){
String[] processList=res.split("\n");
for(String process:processList){
if(process!=null&&process.contains(pid+"")){
String[] infoList=process.split(" ");
processName=infoList[infoList.length-1];
return processName;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return processName;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: