Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将shell脚本连接到android应用程序的最佳方式是什么?_Android_Api_Shell - Fatal编程技术网

将shell脚本连接到android应用程序的最佳方式是什么?

将shell脚本连接到android应用程序的最佳方式是什么?,android,api,shell,Android,Api,Shell,目前,我使用终端仿真器应用程序在android上运行shell脚本命令。需要准备一个应用程序从我的android应用程序调用shell脚本,并在我的应用程序上显示shell脚本的结果。有人能建议最好的方法吗 使用的代码来自 public void runShellCommand(字符串[]cmds)引发异常{ 进程p=Runtime.getRuntime().exec(“su”); DataOutputStream os=新的DataOutputStream(p.getOutputStream(

目前,我使用终端仿真器应用程序在android上运行shell脚本命令。需要准备一个应用程序从我的android应用程序调用shell脚本,并在我的应用程序上显示shell脚本的结果。有人能建议最好的方法吗

使用的代码来自

public void runShellCommand(字符串[]cmds)引发异常{
进程p=Runtime.getRuntime().exec(“su”);
DataOutputStream os=新的DataOutputStream(p.getOutputStream());
InputStream is=p.getInputStream();
用于(字符串临时:cmds){
操作系统写入字节(临时+“\n”);
int read=0;
字节[]buff=新字节[4096];
//如果cmd需要输出
//由于读取的阻塞行为(…)
布尔值CmdRequiresAnutput=true;
如果(CmdRequiremesOnutput){
while(is.available()0){
read=is.read(buff);
如果(读)
    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();
}