简体   繁体   中英

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!

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM