Java如何操作Android的adb shell

Android在操作一些命令的时候需要进入到adb shell,然后才可以执行其他的命令,那么如何做到这一点呢?就要把adb shell按一个Process执行,然后再去操作这个Process的输入和输出流。

代码如下:

这段代码是操作 cmd.exe的

try {

           Process process = Runtime.getRuntime().exec("cmd.exe");

           OutputStream outputStream = process.getOutputStream();

           final InputStream inputStream = process.getInputStream();

           new Thread(new Runnable(){

                byte[] cache = new byte[1024];

                public void run() {

                    System.out.println("listener started");

                    try {

                        while(inputStream.read(cache)!=-1){

                           System.out.println(new String(cache));

                    }

                    } catch (IOException e) {

                       //e.printStackTrace(); 

                    }

                }

           }).start();

            outputStream.write(new byte[]{'d','i','r','\n'});

           //inputStream.

           int i = process.waitFor();

           System.out.println("i=" + i);

       } catch (Exception e) {

           e.printStackTrace();

       }

 

 这段代码是操作Android  adb shell 的

try {

           Process process = Runtime.getRuntime().exec("adb shell");  //adb shell

           final BufferedWriter outputStream = new BufferedWriter(newOutputStreamWriter(process.getOutputStream()));

           final BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

           //这里一定要注意错误流的读取,不然很容易阻塞,得不到你想要的结果,

          final  BufferedReader  errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

           new Thread(new Runnable() {

               String line;

 

                public void run() {

                   System.out.println("listener started");

                    try {

                        while((line=inputStream.readLine()) != null) {

                           System.out.println(line);

                       }

                    } catch (IOException e) {

                       //e.printStackTrace();  

                    }

                }

           }).start();

           new Thread(new Runnable() {

                final BufferedReader br = newBufferedReader(new InputStreamReader(System.in));

 

                public void run() {

                   System.out.println("writer started");

                    String line;

                    try {

                        while ((line =br.readLine()) != null) {

                           outputStream.write(line + "\r\n");

                           outputStream.flush();

                        }

                    } catch (IOException e) {

                       //e.printStackTrace();  

                    }

                }

           }).start();

           int i = process.waitFor();

           System.out.println("i=" + i);

       } catch (Exception e) {

           e.printStackTrace();

       }

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值