gpt4 book ai didi

java - Runtime.exec 和 Android 的问题

转载 作者:行者123 更新时间:2023-11-29 08:13:56 26 4
gpt4 key购买 nike

当我在我的 Android 设备上使用 Runtime.exec 时遇到问题,只是无法弄清楚为什么会发生...

这是我做的测试的一个例子:

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Runtime runtime = Runtime.getRuntime();
for(int i=0; i< 20; i++){
Log.d("TESTEXEC", "EXEC N°"+i+" : BEGIN");
try {
Process process = runtime.exec("/system/bin/ps");
process.getErrorStream().close();
process.getOutputStream().close();
process.getInputStream().close();
process.waitFor();
} catch (Throwable e) {
e.printStackTrace();
}
Log.d("TESTEXEC", "EXEC N°"+i+" : END");
}
}
}

所以,基本上,当这个 fragment 起作用时,它会打印出这个:

EXEC N°0 : BEGIN
EXEC N°0 : END
EXEC N°1 : BEGIN
EXEC N°1 : END
...
EXEC N°19 : BEGIN
EXEC N°19 : END

但不是那样,有时(看起来有点随机),它只是打印

EXEC N°0 : BEGIN

然后一切都锁定了...整个事情似乎崩溃到 runtime.exec("...") 中,并且 process.getErrorStream.close() 从未被调用。另一件奇怪的事:错误总是发生在第一个 exec 上。

这是我使用 DDMS 得到的结果: DDMS

我还检索了/data/anr/traces.txt,但它并不是很有用:

----- pid 11489 at 2011-05-16 15:15:10 -----
Cmd line: com.testexec

DALVIK THREADS:
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x4001d8d0 self=0xcca0
| sysTid=11489 nice=0 sched=0/0 cgrp=unknown handle=-1345021856
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:224)
at java.lang.Runtime.exec(Runtime.java:196)
at java.lang.Runtime.exec(Runtime.java:285)
at java.lang.Runtime.exec(Runtime.java:218)
at com.testexec.MainActivity.onCreate(MainActivity.java:17)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2681)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)

"java.lang.ProcessManager" daemon prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x460969f0 self=0x240f58
| sysTid=11496 nice=0 sched=0/0 cgrp=unknown handle=2363544
at java.lang.ProcessManager.watchChildren(Native Method)
at java.lang.ProcessManager$1.run(ProcessManager.java:87)

"Binder Thread #2" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x460895a0 self=0x1248e0
| sysTid=11495 nice=0 sched=0/0 cgrp=unknown handle=1236304
at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #1" prio=5 tid=6 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x46086cf8 self=0x143480
| sysTid=11494 nice=0 sched=0/0 cgrp=unknown handle=1324096
at dalvik.system.NativeStart.run(Native Method)

"Compiler" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x46084558 self=0x144560
| sysTid=11493 nice=0 sched=0/0 cgrp=unknown handle=1153256
at dalvik.system.NativeStart.run(Native Method)

"JDWP" daemon prio=5 tid=4 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x460842a0 self=0x12dc10
| sysTid=11492 nice=0 sched=0/0 cgrp=unknown handle=1152968
at dalvik.system.NativeStart.run(Native Method)

"Signal Catcher" daemon prio=5 tid=3 RUNNABLE
| group="system" sCount=0 dsCount=0 s=N obj=0x460841e8 self=0x124d00
| sysTid=11491 nice=0 sched=0/0 cgrp=unknown handle=1199296
at dalvik.system.NativeStart.run(Native Method)

"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x441e2ee8 self=0x11fdd8
| sysTid=11490 nice=0 sched=0/0 cgrp=unknown handle=1185776
at dalvik.system.NativeStart.run(Native Method)

----- end 11489 -----

我现在有点绝望,所以如果有人有想法,我将不胜感激。

最佳答案

这很可能是此 Github commit 中修复的错误.本质上,它在 fork() 之后但在执行实际的 exec() 之前直接死锁,因为有人试图 malloc。

关于java - Runtime.exec 和 Android 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com