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

发布于 2024-09-06 01:50:33 字数 386 浏览 3 评论 0原文

我正在尝试从我的代码中执行命令, 命令是“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情丝乱 2024-09-13 01:50:33

手机需要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();
}    

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();
}    
几度春秋 2024-09-13 01:50:33

如果您只是想设置亮度,为什么不通过 提供的 API(又名,您尝试这样做是否有原因)。

int brightness = 125; 
Settings.System.putInt(
      ftaContext.getContentResolver(), 
      Settings.System.SCREEN_BRIGHTNESS, 
      brightness); 

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).

int brightness = 125; 
Settings.System.putInt(
      ftaContext.getContentResolver(), 
      Settings.System.SCREEN_BRIGHTNESS, 
      brightness); 
罪歌 2024-09-13 01:50:33

同意您可能需要 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:

独自唱情﹋歌 2024-09-13 01:50:33

您还可以使用写入权限来remout /system..

You also may remout /system with permissions to write..

橘亓 2024-09-13 01:50:33

我认为该设备需要“扎根”才能正常工作。在谷歌上搜索一下,看看其他开发者是如何做到这一点的,不乏手电筒应用程序。

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.

旧人九事 2024-09-13 01:50:33

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文