android:如何从代码中运行 shell 命令
我正在尝试从我的代码中执行命令, 命令是“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[] 中,以防空格引起问题,但没有太大区别。
有谁知道这个问题有什么解决方法吗?
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");
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
手机需要root,之后你可以执行以下操作:
The phone needs to be rooted, afterwards you can do something like:
如果您只是想设置亮度,为什么不通过 提供的 API(又名,您尝试这样做是否有原因)。
If you're just trying to set brightness, why don't you do so through the provided API (AKA, is there a reason you are trying to do it the way you are).
同意您可能需要 root 手机才能写入系统文件。我很惊讶亮度没有通过 SDK 暴露出来。
有关从代码运行 shell 命令的详细信息,查看这个项目:
Agreed you probably need to root the phone to write to system files. I'm surprised the brightness isn't exposed through the SDK.
For details on running shell commands from code, check out this project:
您还可以使用写入权限来remout /system..
You also may remout /system with permissions to write..
我认为该设备需要“扎根”才能正常工作。在谷歌上搜索一下,看看其他开发者是如何做到这一点的,不乏手电筒应用程序。
I think the device will need to be "rooted" for this to work. Have a search on google and see how other developers have dont this, there is no shortage of flashlight apps.
adb shell 可以在没有 root 设备的情况下充当超级用户。
这是一个调试桥。你可以通过它做任何你想做的事。
但是,当您调用 Runtime.getRuntime().exec 时,您没有相同的权限。有些 shell 命令甚至无法从 exec 中获得。
因此,您不仅需要有 root 权限的设备,还需要 su 前提。
The adb shell can behave as a superuser without rooted devices.
it's a debug bridge. you can do whatever you want through it.
BUT, when your calling Runtime.getRuntime().exec, you don't have the same premissions. some shell commands aren't even available from exec.
so not only you need a rooted device, you also need su premmisions.