Android Runtime.getRuntime().exec
我正在阅读有关如何获取 android 中正在运行的进程的 PID,该进程不在运行 android 2.2 的自定义设备(中文框)上的 dalvik 内部运行(例如,后台本机进程),并且似乎没有本机方法可以获取在java中执行此操作。
我能想到的唯一方法是执行“ps”并解析输出,但我遇到了一个奇怪的问题。
如果我运行 Runtime.getRuntime().exec("ps ") 在这种情况下,一个 ndk 编译的 mplayer 端口,在随机的一段时间后,该命令永远不会返回。
代码如下:
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();
然后对输出缓冲区进行一些解析,并查看输出中是否存在 mplayer。
这在一段时间内(随机时间段)效果很好,比如从 1 小时到 3-4 小时,当代码在尝试 .exec() 时突然停止工作,
如果我通过带有 .postDelayed 的可运行程序在主线程中运行它,那么它可以运行多次,显然会破坏主线程。
如果我在单独的线程上运行它,我会得到相同的行为,当然不会阻塞主应用程序线程。
我每秒、每 5 秒、每 10 秒都运行一次检查,结果相同。
我注意到一件奇怪的事情(不确定是否是问题结果的原因)是当 exec 不工作时,通过在 adb shell 中发出 ps,我可以看到我的 2 个正在运行的应用程序,一旦我杀死了最近,它又开始工作了。
还有其他人遇到过类似的问题吗?我不确定是否是盒子的 android 有问题,这是一个编码问题(可能与我没有做的 exec 有关)或者是否有另一种方法,缺少 NDK 编译的后台应用程序并通过 IPC 与其通信检查该进程是否正在运行。
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.
The only way i could think of is to execute "ps " and parse the output, but i have encountered an odd problem.
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.
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.
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()
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.
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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用服务每 5 秒检查一次,那么如果您想防止服务崩溃时自动启动,则必须从重写方法
onStartCommand()
返回START_NOT_STICKY
。If you are using service to check every 5 seconds then you must return
START_NOT_STICKY
from override methodonStartCommand()
if you want to prevent it start service automatically when it crashes.