Running Shell commands though java code on Android?

To run root commands, you have to use the flllowing format:

    public void RunAsRoot(String[] cmds){
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());            
            for (String tmpCmd : cmds) {
                    os.writeBytes(tmpCmd+"\n");
            }           
            os.writeBytes("exit\n");  
            os.flush();
}

where you pass in an array of strings, each string being a command that needs to be executed. For example:

String[] commands = {"sysrw", "rm /data/local/bootanimation.zip", "sysro"};

Leave a Comment