简体   繁体   English

Android Runtime.getRuntime()。exec

[英]Android Runtime.getRuntime().exec

i was reading around on how to get the PID of a running process in android, that is not running inside dalvik (eg, background native process) on a custom device (chinese box) running android 2.2, and it seems there's no native way to do this in java. 我正在阅读有关如何获取android中正在运行的进程的PID的信息,该ID不在运行android 2.2的自定义设备(中文框)的dalvik(例如后台本机进程)内部运行,并且似乎没有本机的方法可以用java做到这一点。

The only way i could think of is to execute "ps " and parse the output, but i have encountered an odd problem. 我能想到的唯一方法是执行“ ps”并解析输出,但是我遇到了一个奇怪的问题。

If i run Runtime.getRuntime().exec("ps ") in this very case a ndk-compiled mplayer port, after a random period of time, the command never returns. 如果我在这种情况下运行ndk编译的mplayer端口,则运行Runtime.getRuntime()。exec(“ ps”),则经过一段随机时间后,该命令将永不返回。

Here's the code: 这是代码:

Process process = null;
process = Runtime.getRuntime().exec("ps mplayer");
BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()),8192);

char[] buffer = new char[8192];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
      output.append(buffer, 0, read);
}
reader.close();
process.getErrorStream().close();
process.getOutputStream().close();
process.waitFor();

then do some parsing of the output buffer and see if mplayer exists in the output. 然后对输出缓冲区进行一些解析,看看输出中是否存在mplayer。

This works great for some time, random period, say from 1 hours to 3-4 hours, when the code suddenly stop working when trying to .exec() 这在一段随机的时间段(例如1小时到3-4小时)内有效,当代码尝试使用.exec()时突然停止工作

If i run it in the main thread via a runable with .postDelayed so it can run many times, it obviously breaks the main thread. 如果我通过带有.postDelayed的可运行程序在主线程中运行它,使其可以运行多次,则它显然会破坏主线程。

If i run it on a separate thread, i get the same behaviour, of course not blocking the main app thread. 如果我在单独的线程上运行它,我将得到相同的行为,当然不会阻塞主应用程序线程。

I have been running the check every second, every 5 seconds, every 10 seconds with the same result. 我已经每秒,每5秒,每10秒运行一次检查,结果相同。

One odd thing i noticed that (not sure if its the cause of the result of the problem) is when the exec is not working, by issuing a ps in adb shell, i can see 2 running applications of mine, and once i kill the most recent, it starts working again. 我注意到一个奇怪的事情(不确定是否是导致问题的结果的原因)是当exec无法正常工作时,通过在adb shell中发出ps,我可以看到我的2个正在运行的应用程序,并且一旦我杀死了最近,它又开始工作了。

Has anybody else encountered a similar problem ? 还有其他人遇到过类似的问题吗? I'm not sure if it's the box's android that's at fault, it's a coding problem (maybe something related to exec i'm not doing) or if there's another way, short of having a NDK compiled background app and communicate with it via IPC to check if that process is running or not. 我不确定是盒子的android出了问题,还是编码问题(也许是与exec相关的问题,我没有在做),或者还有另一种方法,就是缺少NDK编译后台应用程序并通过IPC与之通信检查该进程是否正在运行。

如果您使用服务每隔5秒检查一次,则如果要防止服务崩溃时自动启动服务,则必须从覆盖方法onStartCommand()返回START_NOT_STICKY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android对Runtime.getRuntime()。exec()的权限 - Android Permissions on Runtime.getRuntime().exec() Java Android Runtime.getRuntime().exec() - Java Android Runtime.getRuntime().exec() Android Runtime.getRuntime()。exec和rsync - Android Runtime.getRuntime().exec and rsync Android-交互式外壳程序(Runtime.getRuntime()。exec()) - Android - Interactive shell (Runtime.getRuntime().exec()) 使用Runtime.getRuntime()。exec()在Android上执行“状态”时出现IOException吗? - IOException when using Runtime.getRuntime().exec() to exec “stat” on Android? android 7:使用Runtime.getRuntime()。exec在APK上安装NullPointerException - android 7: NullPointerException on APK installing with Runtime.getRuntime().exec Android Runtime.getRuntime()。exec()导航目录 - Android Runtime.getRuntime().exec() to nav through directories 有关Android应用中Runtime.getRuntime()。exec()命令用法的帮助 - Help regarding Runtime.getRuntime().exec() command usage in Android app Android JUnit未捕获Runtime.getRuntime()。exec()的IOException - IOException from Runtime.getRuntime().exec() not caught by Android JUnit android runtime.getruntime().exec() 获取进程id - android runtime.getruntime().exec() get process id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM