c# - 根 Android shell 使用 Runtime().Exec ("command"卡在 ReadLine() 上)

标签 c# android android-intent xamarin xamarin.android

请看下面我的代码。

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/

相关文章:

c# - 正则表达式组没有得到所有的字符串

java - chrome 应用程序和 pc 可执行软件之间的区别

android - 打开免打扰设置的目的是什么?

android - 如何将其他 Intent 值传递给 ACTION_OPEN_DOCUMENT_TREE Intent

c# - 双重/不完整的参数 Url 编码

c# - 如何定义和执行约束分层实体的复杂规则

android - MvvmCross 自定义微调器保持默认外观

java - 如何初始化整个应用程序所需的对象?安卓

Android - 是否有一种万无一失的方法来只显示可能的 EMAIL 客户端?

c# - 传递给在紧密循环中生成的线程的参数