如何从code运行shell命令

如何从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, 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("exit\n");
    os.flush();
    os.close();

    process.waitFor();
}

这篇关于机器人:如何从code运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:48