Question

I am working on a rooted Android device. Is there a way to have linux commands executed as superuser without having to invoke

Runtime.getRuntime().exec("su")

every time? What bothers me is the Toast which appears every time saying that the app has been granted superuser permission. The first time I issue a su command a dialog appears and even if I select to remember the choice every time the command is executed I get the Toast.

Was it helpful?

Solution

Nope there is no way while using the default su binary as supplied. Both chainxx and chainfire's SU apps have options to not have the popup but thats the users choice.

Of course you could always compile your own su binary and use that, it is open source. This could leave to security concerns having a open su binary, users will probably not like this. If you do you will want to do something like how the other su binaries operate in where its checks to see if it your application doing the calling if not refuse operation. But again this is probably not something you should do.

OTHER TIPS

Not sure what you want to execute and the frequency. But you can execute more than one command in a OutputStream, so you could queue up your commands in an array or something like that.

public void runCommandsAsRoot(String[] cmds) throws IOException {
    Process proc = Runtime.getRuntime().exec("su");

    DataOutputStream oStream = new DataOutputStream(proc.getOutputStream());

    for (String cmd : cmds) {
        oStream.writeBytes(cmd + "\n");
    }

    oStream.writeBytes("exit\n");  
    oStream.flush();
}

Then invoke it like this:

String[] commands = { "fistCommand", "secondCommand" };
runCommandsAsRoot(commands);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top