0
echo 0 > /sys/class/leds/button-backlight/brightness

The above command is working perfectly through adb shell.But when I try to run through my code there is no effect.Here is my code.

Process mSuProcess;
mSuProcess = Runtime.getRuntime().exec("su"); 
DataOutputStream mSuDataOutputStream = new DataOutputStream(mSuProcess.getOutputStream()); 
mSuDataOutputStream.writeBytes("echo 0 > /sys/class/leds/button-backlight/brightness \n");
mSuDataOutputStream.flush();
mSuDataOutputStream.close();

Please help me out on this.

Mihir
  • 2,064
  • 2
  • 21
  • 28
  • 1
    Although you say you are 'rooted' that does not necessarily mean you have an `su` binary usable for this purpose. See if you can manage to capture the stdout /stderr of the su process. Also consider installing something like connectbot and trying your commands in an interactive shell on the device, which will run as an application userid and thus give you a more comparable case than adb does. – Chris Stratton May 22 '13 at 15:28

2 Answers2

2

If the device which you are working on is not rooted then this may not work..

mn0102
  • 839
  • 1
  • 12
  • 25
  • Run shell instead of su, like; Process mSuProcess; mSuProcess = Runtime.getRuntime().exec("sh"); – mn0102 May 22 '13 at 09:58
  • try this one please mSuProcess.getOutputStream().writeBytes("echo 0 > /sys/class/leds/button-backlight/brightness \n"); and keep it through "su" – mn0102 May 22 '13 at 10:09
  • Refer [this](http://stackoverflow.com/questions/6882248/running-shell-commands-though-java-code-on-android).. hopefully will help you – mn0102 May 22 '13 at 10:31
  • Your code should work.. i dont see any reason why this is happening.. best luck – mn0102 May 22 '13 at 10:39
1

The google original su binary checks for UID and only works if run by root or shell users. This is why it works when you run your command from adb shell interactively. In order to run this code from a java app you need to use a patched su binary - without UID checks.

Alex P.
  • 30,437
  • 17
  • 118
  • 169