root access snippet

Quickly share your Android Code Snippets here...

root access snippet

Postby azatoth » Mon Oct 12, 2009 11:07 am

I write this little snippet for a test of root access:

Code: Select all
TextView tv = (TextView) findViewById(R.id.test);
       
      Process p;
      try {
         p = Runtime.getRuntime().exec("su");
         DataOutputStream os = new DataOutputStream(p.getOutputStream());
         os.writeBytes("echo "Hi Mom!!!!!!!!" >/system/sd/ciccio.txtn");
         os.writeBytes("exitn");
         os.flush();
         try {
            p.waitFor();
                 if (p.exitValue() != 255) {
                    tv.setText("OK!");
                 }
                 else {
                    tv.setText("Error");   
                 }
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }


It's only an example.
You must implements exception code ;)
azatoth
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Oct 12, 2009 11:01 am

Top

Postby pekarz » Mon Jan 04, 2010 12:15 pm

Hello

I've tested this code and got an IOExcrption with detail message - Broken Pipe

The same exception was when I'd chenge "su" to "konsole"

What was done wrong?
pekarz
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 04, 2010 12:08 pm

Postby stealthcopter » Sun Jan 17, 2010 11:55 am

Thanks for the code snippet. I just tested this and on a phone with no root access an except is raised, and on one with the root access a dialog pops up asking for permission to allow the application root access.

@pekarz: You cannot use 'konsole' as that is a KDE specific GUI for the terminal, you would you 'bash' to get access the command line, but this is superfluous as exec() is already running things through the terminal.
stealthcopter
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun Jan 17, 2010 11:42 am

Re: root access snippet

Postby rustyx » Tue May 28, 2013 4:33 pm

Is there a way to run an Android app as root using something like this?
Can I exec() dalvik VM this way?
rustyx
Once Poster
Once Poster
 
Posts: 1
Joined: Tue May 28, 2013 4:31 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 5 guests