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

       }

    }


更多相关文章

  1. android EditText设置不可写
  2. android 使用html5作布局文件: webview跟javascript交互
  3. android studio调试c/c++代码
  4. IM-A820L限制GSM,WCDMA上网的原理(其他泛泰机型可参考)7.13
  5. 锁屏界面
  6. android(NDK+JNI)---Eclipse+CDT+gdb调试android ndk程序
  7. Android(安卓)version and Linux Kernel version
  8. Android(安卓)闹钟管理类的使用
  9. Android学习篇之Menu的使用

随机推荐

  1. Android(安卓)6.0 权限请求问题
  2. Android音视频处理技术 视频录制
  3. 塔防游戏《android防御战》上线了
  4. 《Android 开发入门》我认识中的Android
  5. 2013 android经典实例及赚钱心得
  6. XMPP协议实现原理介绍
  7. Android周学习Step By Step(5)--常用widget
  8. 深入浅出 - Android系统移植与平台开发(六
  9. Android 三大图片缓存原理、特性对比
  10. Android调用系统相机、自定义相机、处理