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 ?
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();
}
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:
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);
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.
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.