Execute Shell Command No Return - Android Android OS

Android examples for Android OS:Shell Command

Description

Execute Shell Command No Return

Demo Code


//package com.java2s;

import java.io.DataOutputStream;

public class Main {
    private static Process sh = null;
    private static Process su = null;

    public static void ExecuteNoReturn(String command, Boolean useroot,
            boolean forcenew) throws Exception {

        Process p;/*w  w  w.  j a  va2s  .  c o  m*/
        DataOutputStream os;

        p = getProcess(useroot, forcenew);

        os = new DataOutputStream(p.getOutputStream());

        os.writeBytes(command + "\n");

        if (forcenew) {
            os.writeBytes("exit\n");
            os.flush();
            os.close();
        }

        //p.waitFor();
    }

    private static Process getProcess(boolean useroot, boolean forcenew)
            throws Exception {

        if (forcenew && useroot)
            return Runtime.getRuntime().exec(new String[] { "su" });
        else if (forcenew && !useroot)
            return Runtime.getRuntime().exec(new String[] { "sh" });
        else if (useroot && su != null)
            return su;
        else if (useroot && su == null) {
            su = Runtime.getRuntime().exec(new String[] { "su" });
            return su;
        } else if (!useroot && sh != null)
            return sh;
        else if (!useroot && sh == null) {
            sh = Runtime.getRuntime().exec(new String[] { "sh" });
            return sh;
        }

        /* Shouldn't ever actually get here! */
        return Runtime.getRuntime().exec(new String[] { "sh" });

    }
}

Related Tutorials