-2

To use sudo feature, what should I write in the my application? Should I write something? If yes, can you tell me how I can write sudo application? Do I need to change manifest.xml, or add some Java code?

1 Answer 1

1

Assuming the device is rooted and your app has been granted superuser permissions, you can use the following method to run commands as root:

public static void runAsRoot(String[] cmds){
        Process p;
        try {
            p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());  
            BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream()));
            for (String tmpCmd : cmds) {
                os.writeBytes(tmpCmd+"\n");
                String test;
                while((test = bf.readLine()) != null)
                {
                    Log.i(TAG, test);
                }
            }

            //os.writeBytes("exit\n");  
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Just pass it a list of commands in a String array.

4
  • 1
    ' your app has been granted superuser permissions' how ?
    – jaudo
    Jan 17, 2013 at 14:06
  • Normally, on a rooted device a dialog will popup asking the user whether or not this app should be given root access. The user must agree to give you access. Jan 17, 2013 at 14:07
  • List of the command supported by the Android device by default .
    – jaudo
    Jan 17, 2013 at 14:43
  • There is a pretty complete list here Jan 17, 2013 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.