android process.getinputstream(),process - Android Runtime.getRuntime().exec - Stack Overflow

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值