Java 从我的应用程序运行shell实用程序会导致它使用100%的CPU
这有点奇怪: 我正在尝试从我的android应用程序运行终端命令。该命令启动一个在后台运行的小型linux实用程序。当我从终端模拟器启动它时,它工作正常,没有异常的CPU活动,但是当我从我的应用程序运行相同的命令时,它启动的实用程序使用了大量的CPU。另外,另一个进程(system/bin/logcat2)出现并开始使用大量CPU 下面是我用来发出shell命令的代码:Java 从我的应用程序运行shell实用程序会导致它使用100%的CPU,java,android,Java,Android,这有点奇怪: 我正在尝试从我的android应用程序运行终端命令。该命令启动一个在后台运行的小型linux实用程序。当我从终端模拟器启动它时,它工作正常,没有异常的CPU活动,但是当我从我的应用程序运行相同的命令时,它启动的实用程序使用了大量的CPU。另外,另一个进程(system/bin/logcat2)出现并开始使用大量CPU 下面是我用来发出shell命令的代码: void execCommandLine(String command) { Runtime run
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();
首先,感谢您的回复。我不仅仅是在运行“su”,而是将命令传递给在执行“su”之后运行的方法。你能进一步解释一下你发布的代码片段吗?