如何从通过 getRuntime().exec 运行的二进制文件中获取 pid。我想从/data/data/com.tes.tes/binary
我运行该服务的代码是:
MyExecShell("/data/data/com.tes.tes/binary");
public void MyExecShell(String cmd) {
Process p = null;
try {
p = Runtime.getRuntime().exec(cmd);
p.waitFor();
} catch (Exception e) {
// TODO: handle exception
}
}
如果我运行命令ps | grep binary
,我会得到结果:
app_96 12468 1 1176 680 c0194d70 0007efb4 S /data/data/com.tes.tes/binary
我想得到pid,怎么办?我试过这个:
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = manager.getRunningAppProcesses();
if (list != null) {
for (int i = 0; i < list.size(); ++i) {
Log.d("DLOG", list.get(i).toString() + "\n");
if ("/data/data/com.tes.tes/binary"
.matches(list.get(i).toString())) {
int pid = android.os.Process.getUidForName("/data/data/com.tes.tes/binary");
Log.d("DLOG","PID: "+pid);
}
}
}
但不是成功。
谢谢。