android:如何从代码中运行shell命令

ee3*_*509 4 shell android command

我试图从我的代码中执行一个命令,命令是"echo 125> /sys/devices/platform/flashlight.0/leds/flashlight/brightness",我可以运行它而没有adb shell的问题

我正在使用Runtime类来执行它:

Runtime.getRuntime().exec("echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness");
Run Code Online (Sandbox Code Playgroud)

但是我收到权限错误,因为我不应该访问sys目录.我还尝试将命令放在String []中,以防空格导致问题,但它没有太大的区别.

有谁知道任何解决方法吗?

Lox*_*ley 12

手机需要扎根,之后您可以执行以下操作:

public static void doCmds(List<String> cmds) throws Exception {
    Process process = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());

    for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd+"\n");
    }

    os.writeBytes("exit\n");
    os.flush();
    os.close();

    process.waitFor();
}    
Run Code Online (Sandbox Code Playgroud)