作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从我的代码中执行命令, 命令是“echo 125 >/sys/devices/platform/flashlight.0/leds/flashlight/brightness” 我可以毫无问题地从 adb shell 运行它
我正在使用运行时类来执行它:
Runtime.getRuntime().exec("echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness");
但是我得到一个权限错误,因为我不应该访问 sys 目录。 我还尝试将命令放在 String[] 中,以防空格引起问题,但并没有太大区别。
有人知道解决这个问题的方法吗?
最佳答案
手机需要root,之后你可以这样做:
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();
}
关于机器人 : how to run a shell command from within code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3054973/
我是一名优秀的程序员,十分优秀!