安卓(Android) 怎么执行shell命令不返回

2019年10月13日15:25:57安卓(Android) 怎么执行shell命令不返回已关闭评论 612 984字阅读3分16秒

1.简介

安卓(Android) 怎么执行shell命令不返回?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



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;
        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();
        }

        
    }

    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;
        }

        
        return Runtime.getRuntime().exec(new String[] { "sh" });

    }
}

 
继续阅读
编程十万个怎么办