Simon
Simon

Reputation: 364

How to make your android app ask for superuser access only once?

I'm creating my first android app which is changing your MAC address and the device hostname. However, I want to only have to request for superuser access once.

This is how I'm currently executing my su commands:

String[] cmdHost = { "su", "-c", "/system/bin/setprop net.hostname " + strHostname};
execute = Runtime.getRuntime().exec(cmdHost);

Every time the user presses on the button to change the hostname or the MAC address, it asks for superuser access again.

Is there any way I can solve this? Also, I see that some apps only execute /system/bin/sh in the SuperUser app logs. Mine shows the whole command.

Thank you very much!

Upvotes: 2

Views: 2954

Answers (1)

Tim
Tim

Reputation: 6712

Check out the library RootTools - it supports operations like the following:

if (RootTools.isAccessGiven()) { /* magic root code here */ }

You can send commands to the shell like this

try {
    List<String> output = RootTools.sendShell(command);

    String[] commands = new String[] { command1, command2 };
    List<String> otherOutput = RootTools.sendShell(commands);
} catch (IOException e) {
    // something went wrong, deal with it here
}

Upvotes: 5

Related Questions