Android 如何从通过getRuntime().exec运行的二进制文件中获取pid
如何从通过getRuntime().exec运行的二进制文件中获取pid。 我想从Android 如何从通过getRuntime().exec运行的二进制文件中获取pid,android,Android,如何从通过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);
/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);
}
}
}
ActivityManager=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
List=manager.getRunningAppProcesses();
如果(列表!=null){
对于(int i=0;i
但不是成功
谢谢。问题是,正在运行的进程不是应用程序上下文。 您可以尝试通过标准Linux方法获取pid:
private int getPid() {
int pid = -1;
Process p = null;
try {
p = Runtime.getRuntime().exec("ps");
p.waitFor();
InputStream is = p.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String s;
while ((s=r.readLine())!= null) {
if (s.contains("/data/data/com.tes.tes/binary")) {
// TODO get pid from ps output
// like " | awk '{ pring $2 }'
// pid = something;
}
}
r.close();
} catch (Exception e) {
// TODO: handle exception
}
return pid;
}
您好,谢谢您的回复,顺便问一下,是否所有android设备都有二进制“ps”?如果是,我将使用你的答案。再次感谢。据我所知:是的。好的,谢谢。通过运行时exec使用设置编号pid解决了我的问题。