33

I found out that there's so many apps out there which required root access.

How were they able to create those apps? Where did they found all the resource? Is there any official document about the root access? Or will it need some hackish way?

And what kinds of access which required root access? Is proxy one of them? I didn't found any documentation about accessing the proxy anywhere.

=== UPDATE ===

I think some people mistaken by my question. I know that the phone need to be rooted to get root access. but the things I want to find out is, what kind of services are need root access? Some I see wireless tether, the other is to clear cache (is it even need root access just to clear cache?), and the other apps out there. is there any list of what I kind of advantage I get from root access?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Fugogugo
  • 4,460
  • 10
  • 36
  • 50
  • I guess, the question I have is: why do you want to gain root access? What do you want to achieve? Can't you do it without rooting? – Aleadam Mar 25 '11 at 18:58
  • Well. I was just wondering. sometime I found out that there's no way to do things in common way, and I wonder if it can be done by root access. :) – Fugogugo Mar 26 '11 at 04:19

5 Answers5

22

You need su installed in the phone (of course). Details here: http://forum.xda-developers.com/showthread.php?t=682828

And to use it, is as simple as running su command. Here is a sample I use to reboot the phone programmatically (copied from this answer: Android 2.2: Reboot device programmatically )

try {
    Runtime.getRuntime().exec("su");
    Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
} 
Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • And, BTW, to respond to: and what kinds of access which required root access? - The phone needs to be "rooted" first. The method is a little bit "hackish" and it will depend on the model, but it's generally very easy. You can find all the details required for each phone in http://forum.xda-developers.com – Aleadam Mar 25 '11 at 17:52
  • what kind of command I can use? all linux `su- required` command? – Fugogugo Mar 25 '11 at 17:54
  • 1
    Well, the update made it a completely different question :) I can't tell you the list of tasks that require root, but for sure r/w access to /system (as in root explorer or TiBu) or direct access to the devices in /dev or configs in /etc will require it. I think the most direct answer is to allow you to bypass the android api restrictions. I only used it for very basic stuff (lagfix control app for the captivate, which can be found here: http://forum.xda-developers.com/showthread.php?t=917727 ) – Aleadam Mar 25 '11 at 18:55
5

Should work on most versions:

try {
    Runtime.getRuntime().exec("su -c reboot");
} catch (IOException e) {
} 
Dpp
  • 1,926
  • 1
  • 18
  • 26
2

Use this function and use it anywhere of your project

 public static void sudo(String... strings) {
    try {
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        for (String s : strings) {
            outputStream.writeBytes(s + "\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Usage of the above function and use it any type of shell commands with root users

  sudo("iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080");
sanjay
  • 695
  • 11
  • 22
2

Many of the applications depend on a shell script/command prefixed with su.

I do not think there is a way to request another context or elevate privileges in Android API.

tamarintech
  • 1,972
  • 12
  • 17
0

Usually rooted apps are just using commands like "su ...", which they aren't able to use without root access. And with these command executions can they reach everything (i.e. removing a system app or boosting your android device)

gyurix
  • 1,106
  • 9
  • 23