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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3054973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:29:45  来源:igfitidea点击:

android : how to run a shell command from within code

androidshellcommand

提问by ee3509

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

我正在尝试从我的代码中执行一个命令,该命令是“echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness”,我可以从 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.

但是我收到权限错误,因为我不应该访问 sys 目录。我还尝试将命令放在 String[] 中,以防空格引起问题,但没有太大区别。

Does anyone know any workaround for this ?

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

回答by Loxley

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

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

回答by Brad Hein

Agreed you probably need to root the phone to write to system files. I'm surprised the brightness isn't exposed through the SDK.

同意您可能需要 root 手机才能写入系统文件。我很惊讶亮度没有通过 SDK 暴露出来。

For details on running shell commands from code, check outthis project:

有关从代码运行 shell 命令的详细信息,请查看此项目:

回答by JasCav

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

如果您只是想设置亮度,为什么不通过提供的 API设置(又名,您是否有理由按照自己的方式进行设置)。

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

回答by Vlad

The adb shell can behave as a superuser without rooted devices. it's a debug bridge. you can do whatever you want through it.

adb shell 可以作为没有 root 设备的超级用户。这是一个调试桥。你可以通过它做任何你想做的事。

BUT, when your calling Runtime.getRuntime().exec, you don't have the same premissions. some shell commands aren't even available from exec.

但是,当您调用 Runtime.getRuntime().exec 时,您没有相同的权限。某些 shell 命令甚至无法从 exec 获得。

so not only you need a rooted device, you also need su premmisions.

因此,您不仅需要有根设备,还需要 su premmions。

回答by Kemal

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

您也可以删除 /system 具有写入权限。

回答by thomasfedb

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.

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