请看下面我的代码。
public static bool RunRootCommands(List<string> commands)
{
Java.Lang.Process rootProcess;
try
{
rootProcess = Runtime.GetRuntime().Exec("su");
var outputStream = new Java.IO.DataOutputStream(rootProcess.OutputStream);
var inputStream = new Java.IO.DataInputStream(rootProcess.InputStream);
if (inputStream != null && outputStream != null)
{
// This works. Can ReadLine() just fine.
// outputStream.WriteBytes("id\n");
outputStream.WriteBytes("pm disable com.example.app");
string line = inputStream.ReadLine(); // Gets stuck here. Waiting and app lags.
outputStream.WriteBytes("exit\n");
outputStream.Flush();
return true;
}
}
catch { }
return false;
}
我能够获得 root 访问权限。当我运行“id\n”命令时,我能够继续并从 Readline() 取回数据。但是,使用您看到的当前命令,它在常规 shell 上运行完美,但在我的应用程序中不起作用。发生的事情是它卡在 ReadLine() 上,它不显示任何错误,只是卡在那里。我通过 outpustStream 输入的命令被激活。我在没有 Input.ReadLine() 的情况下尝试过,但是,在那种情况下,该命令没有成功激活。也尝试在末尾加上“/n”。
最佳答案
您必须以\n 结束每个命令,否则该命令将不会被执行。
outputStream.WriteBytes("pm disable com.example.app\n");
关于c# - 根 Android shell 使用 Runtime().Exec ("command"卡在 ReadLine() 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397231/