Android在应用程序的上一个命令的上下文中执行shell

时间:2013-11-25 05:34:21

标签: android shell android-context execute

我知道如何在android app中执行shell命令

shell.exec("ls /");

以及如何阅读它的回复

new BufferedReader(new InputStreamReader(_process.getInputStream())).readLine();

但我有一个shell应用程序,运行后需要额外的用户输入。我想知道如何向同一个shell命令发送额外的输入。

例如:

cp /abc/ /a/abc/

并且假设该命令要求用户通过输入额外的Y来确认覆盖,该怎么做?

2 个答案:

答案 0 :(得分:1)

尝试通过shell发送多个命令。这适用于ssh的JSCH客户端

 Channel channel=session.openChannel("shell");
            OutputStream ops = channel.getOutputStream();
            PrintStream ps = new PrintStream(ops, true);

             channel.connect();
             ps.println("mkdir folder"); 
             ps.println("dir");
     //give commands to be executed inside println.and can have any no of commands sent.
                          ps.close();

             InputStream in=channel.getInputStream();
             byte[] bt=new byte[1024];


             while(true)
             {

             while(in.available()>0)
             {
             int i=in.read(bt, 0, 1024);
             if(i<0)
              break;
                String str=new String(bt, 0, i);
              //displays the output of the command executed.
                System.out.print(str);


             }
             if(channel.isClosed())
             {

                 break;
            }
             Thread.sleep(1000);
             channel.disconnect();
             session.disconnect();   
             }

答案 1 :(得分:0)

Process process = Runtime.getRuntime().exec(commandLine);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));