0

我有一个自定义设备 android 4.3。某些命令会出现问题,例如:

su -c 'pm enable com.android.systemui'

当我在 adb 上运行此命令时,它可以工作。但是,当我使用此库以编程方式运行代码时,它不起作用,也没有显示错误。

有趣的观察:

Shell.SU.available() : false
Shell.SU.isSELinuxEnforcing() : false
4

1 回答 1

0

好的,所以设备已植根。您尝试使用该库执行该命令的任何原因?

我想说的是为什么你不能自己运行 shell 命令?

runRootCommand方法:

static boolean runRootCommand(String command) {
    boolean status = true;
    DataOutputStream os = null;

    try {
        Process process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (IOException | InterruptedException e) {
        Log.e(TAG, e.toString());
        status = false;
    } finally {
        try {
            if (os != null)
                os.close();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            status = false;
        }
    }
    return status;
}

然后像这样调用该方法:

boolean success = runRootCommand("pm enable com.android.systemui");

if(success) {
    // command was successful
} else {
    // command was NOT successful
}

这将以“su”(超级用户)身份运行命令。希望这可以帮助。

于 2016-11-01T16:31:45.057 回答