-1

Currently I have shell script commands which I run on android using terminal emulator application. Need to prepare an application to call the shell scripts from my android application show the result of the shell script on my application. Can anybody suggest the best way to do it.

Surjya Narayana Padhi
  • 7,741
  • 25
  • 81
  • 130
  • http://stackoverflow.com/questions/10302433/how-can-i-execute-all-the-possible-unixshell-commands-in-android-programmatica – Merlevede Feb 27 '14 at 02:54

1 Answers1

0

Used code from here

    public void runShellCommand(String[] cmds) throws Exception {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        InputStream is = p.getInputStream();
        for (String temp : cmds) {
            os.writeBytes(temp+"\n");
            int read= 0;
            byte[] buff = new byte[4096];

        // if cmd requires an output
       // due to the blocking behaviour of read(...)
        boolean cmdRequiresAnOutput = true;
        if (cmdRequiresAnOutput) {
            while( is.available() <= 0) {
                try { Thread.sleep(200); } catch(Exception ex) {}
            }

            while( is.available() > 0) {
                read = is.read(buff);
                if ( read <= 0 ) break;
                String str = new String(buff,0,read);
                console.println("#> "+str);
        }
    }
}        
os.writeBytes("exit\n");
os.flush();
}

Sleep is required for commands that use long time.

Community
  • 1
  • 1
Nikhil
  • 80
  • 6