Android 在来自应用程序的先前命令的上下文中执行 shell

标签 android shell android-context execute

我知道如何从 android 应用程序在 shell 中执行命令

shell.exec("ls /");

以及如何从中读取响应

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

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

例如:

cp /abc/ /a/abc/

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

最佳答案

尝试通过 shell 发送多个命令。这可以通过 JSCH 客户端进行 ssh

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

关于Android 在来自应用程序的先前命令的上下文中执行 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185446/

相关文章:

安卓动画卡住

html - 通过 html 按钮(onclick)执行 shell 命令

当从另一个类作为主要 Activity 调用它时,Android Alertdialog 崩溃并出现 "Unable to add window"异常

android - 我怎样才能获得 MAC 地址 android 7.0

javascript - 无法从 android webview 内的网页进行语音通话

java - 奇怪的错误 : Skipping entry 0x106000d in package table 0 because it is not complex

linux - 唯一的文件 ID?

linux - 创建一个基本的 .bashrc 文件

android - 谁能解释将 Activity 上下文传递给内部类与仅使用 MyActivity.this 引用它之间的区别?

android - 将值分配给不同类的 TextView