/sys/devices/platform/flashligh...">

Android: how to run a shell command from code

I am trying to execute a command from my code, the command "echo 125> /sys/devices/platform/flashlight.0/leds/flashlight/brightness" and I can run it without problems from the adb shell

I use the 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 should not access the sys directory. I also tried to put the command in String [] only if the spaces caused the problem, but it did not differ much.

Does anyone know of a workaround for this?

+4
source share
6 answers

The phone should be rooted, after which 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(); } 
+12
source

I agree that you probably need to connect the phone to write to system files. I am surprised that the brightness does not open through the SDK.

For more information on running shell commands from code, check out this project:

+2
source

If you're just trying to set the brightness, why don't you do it through the provided API (AKA, is there a reason why you are trying to do it the way you are).

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

The adb shell can act as root without root devices. This is a debugging bridge. you can do whatever you want through it.

BUT when you call Runtime.getRuntime (). exec, you do not have the same sentences. some shell commands are not even accessible from exec.

therefore, you need not only a rooted device, but you also need preliminary solutions.

+1
source

You can also run the remout / system command with write permissions.

+1
source

I think the device must be β€œrooted” for this to work. Try searching on google and see how other developers do it, there is no shortage of flashlight applications.

0
source

Source: https://habr.com/ru/post/920331/


All Articles