gpt4 book ai didi

java - 从我的应用程序运行 shell 实用程序会导致其使用 100% CPU

转载 作者:行者123 更新时间:2023-12-01 15:53:26 24 4
gpt4 key购买 nike

这有点奇怪:

我正在尝试从我的 Android 应用程序运行终端命令。该命令启动一个在后台运行的小型 Linux 实用程序。当我从终端模拟器启动它时,它工作正常,没有异常的 CPU Activity ,但是当我从我的应用程序运行相同的命令时,它启动的实用程序会使用大量 CPU。此外,另一个进程 (system/bin/logcat2) 也会出现并开始使用大量 CPU。

这是我用来发出 shell 命令的代码:

 void execCommandLine(String command)
{
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;

try
{
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
}
catch (IOException ex)
{

Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command);
return;
}
finally
{
if (osw != null)
{
try
{
osw.close();
}
catch (IOException e){}
}
}

try
{
proc.waitFor();
}
catch (InterruptedException e){}

if (proc.exitValue() != 0)
{

Log.e("execCommandLine()", "Command returned error: " + command + "\n Exit code: " + proc.exitValue());
}
}

有人知道这是怎么回事吗?

最佳答案

运行“just”su 不会有太大帮助(它将默认为交互式)

如果使用另一个测试命令(例如 ls)问题仍然存在,您可能需要

int exitVal = proc.waitFor();

第一

关于java - 从我的应用程序运行 shell 实用程序会导致其使用 100% CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544874/

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