机器人:如何从code运行shell命令机器人、命令、code、shell

由网友(旅行写序)分享简介:我想从我的code执行命令,该命令是呼应125> /sys/devices/platform/flashlight.0/leds/flashlight/brightness我可以运行而无需从亚行的shell问题I am trying to execute a command from within my code,t...

我想从我的code执行命令,  该命令是呼应125> /sys/devices/platform/flashlight.0/leds/flashlight/brightness  我可以运行而无需从亚行的shell问题

I am trying to execute a command from within my code, the command is "echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness" and I can run it without problems from adb shell

我使用运行时类来执行它:

I am using Runtime class to execute it :

Runtime.getRuntime().exec("echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness");

不过,我得到一个权限错误,因为我不应该访问SYS目录。  我也试图在一个String []以防万一空间造成的问题将命令,但它并没有多大differense。

However I get a permissions error since I am not supposed to access the sys directory. I have also tried to place the command in a String[] just in case spaces caused a problem but it didn't make much differense.

没有人知道任何解决方法吗?

Does anyone know any workaround for this ?

推荐答案

手机需要植根,事后你可以这样做:

The phone needs to be rooted, afterwards you can do something like:

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("exitn");
    os.flush();
    os.close();

    process.waitFor();
}    
阅读全文

相关推荐

最新文章