java - 使用 root Android 应用程序运行二进制文件

标签 java android unix android-ndk native

我想在 /dev/local 运行一个名为 native 的二进制文件(我通过 adb 推送它),具有 root 权限。 为了实现这一点,我编写了以下代码:

    try {
        root=Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(root.getOutputStream());
        DataInputStream osRes = new DataInputStream(root.getInputStream());
        os.writeBytes("/data/local/native\n");
        os.flush();
        TextView output=(TextView)findViewById(R.id.textview);
        output.append(osRes.readLine());
        root.waitFor();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, e.toString() , Toast.LENGTH_SHORT).show();
    }

这给了我一个NullPointerException。所以,我尝试将其更改为:

    try{
        root=Runtime.getRuntime().exec("su");
        root.waitFor();
        DataOutputStream os = new DataOutputStream(root.getOutputStream());
        DataInputStream osRes = new DataInputStream(root.getInputStream());
        os.writeBytes("/data/local/native\n");
        os.flush();
        TextView output=(TextView)findViewById(R.id.textview);
        output.append(osRes.readLine());
        root.waitFor();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, e.toString() , Toast.LENGTH_SHORT).show();
    }

有了这个,我得到了 BrokenPipe 错误。

请帮助我,我想以 root 权限运行二进制文件,有没有其他方法或者我做错了什么?

最佳答案

尝试运行 su -c/path/to/executable

关于java - 使用 root Android 应用程序运行二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6446969/

相关文章:

java - buildr:将依赖包打包到一个 jar 中

java - Android Xml 处理程序仅保留我的 xml 文件中的最后一个对象

unix - sed 丢失了该行的右侧部分

android - 如何避免使用 getIdentifier 检索文本资源?

java - Android 应用程序在重新打开和替换 fragment 时崩溃

unix - 如何在unix中按字母和数字在不同字段上对文件进行排序?

linux - 使用文件内容创建新目录

java - 在 JavaFX 中切换 View

java - 如何在操作栏下方添加按钮

android - Android 中错误的订阅到期日期