Running a shell utility from my app causes it to use 100% CPU
This is sort of an odd one:
I am trying to run a terminal command from my android app. The command launches a small linux utility that runs in the background. When I launch i开发者_Go百科t from a terminal emulator it works fine, with no unusual CPU activity, but when I run the same command from my app, the utility that it launches uses a ton of CPU. Also, another process (system/bin/logcat2) shows up and starts using a bunch of CPU as well.
Here is the code I am using to issue the shell commands:
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());
}
}
Anyone know what's going on?
Running 'just' su is not going to be very helpful (it will default to being interactive)
If the problem persists with another test command, like ls, you may want to
int exitVal = proc.waitFor();
first
精彩评论