Run a shell command as root on android?

2019-08-04 13:23发布

问题:

I thought it would be cool to turn the hardware button lights on when my application needs user attention so the user could get an indicator when the screen is blanked. As it turns out, this requires root access (echo 1 > /system/class/leds/keyboard-backlight/brightness). I found a link on Stack (http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html) to a class which tests for root access but it fails on my Nexus-S.

Is there a way to run shell commands as root without having to root the device?

Thanks

Edit: this is the error I'm getting from logcat 11-15 12:34:19.889: D/ROOT(2029): Root access rejected [java.io.IOException] : Error running exec(). Command: [su] Working Directory: null Environment: null

回答1:

If your phone has root access then you can fire shell scripts using Process and Runtime class..



回答2:

The reason it fails it because your phone needs to be rooted.

See the Wikipedia entry for Rooting (Android OS).

It would be a major security flaw if any app on any phone could take root and do anything.