6

im trying to find the method behind having superuser access on android devices. basically i want my app to be able to remove system files, and so asking the user if its ok after the app is launched like most other apps, and then what code exactly am i looking to use to perform these sorts of actions? cant find much on google

thanks in advance!

sg86-
  • 75
  • 1
  • 5

3 Answers3

10

If the device is rooted, you can use the following code to gain access to a root shell:

    try
    {
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); 
        DataInputStream inputStream = new DataInputStream(process.getInputStream());

        outputStream.writeBytes(command + "\n");
        outputStream.flush();

        outputStream.writeBytes("exit\n"); 
        outputStream.flush(); 
        process.waitFor();
    }
    catch (IOException e)
    {
        throw new Exception(e);
    }
    catch (InterruptedException e)
    {
        throw new Exception(e);
    }

Where "command" is the command that you want to execute.

Blu Dragon
  • 394
  • 5
  • 14
  • where did you find this resource my friend? is there any documentation for it? im guessing i just use the command variable each time i want to run a command that needs this access? – sg86- Sep 06 '10 at 18:45
  • Glad to be of help! I can't remember where I found that code but I know it took a lot of Googling. The sample was taken from an app that I am currently working on. To run multiple commands, just do "outputStream.writeBytes("command\n");" and "outputStream.flush();" for each command. You can also create a list of command to loop over. – Blu Dragon Sep 06 '10 at 19:32
1

If I'm getting it right you want to grant your application root privileges. This is not possible with the standard Android image which is installed on the devices. The su command does not support changing to root.

You can however load a customized rooted ROM on your device other than that I'm afraid there is no solution.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • 1
    i use many many applications that ask for super user permissions before launching so not sure what your on about – sg86- Sep 06 '10 at 18:44
  • Sure you can ask for super user permission but on a default not rooted device with a default untouched ROM this won't work. Thats what I wanted to say. – Octavian Helm Sep 06 '10 at 19:08
1

Here you can find a clear illustration about how to gain root permissions

Searene
  • 25,920
  • 39
  • 129
  • 186