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;
}
根据进程ID获取进程名字
最新推荐文章于 2024-09-11 09:05:11 发布