【问题标题】:How to terminate adb logcat in java application如何在java应用程序中终止adb logcat
【发布时间】:2018-02-24 04:49:33
【问题描述】:

你好我要开发adb日志捕获工具,我用下面的代码试过了

process = Runtime.getRuntime().exec("adb logcat -c");
process = Runtime.getRuntime().exec("adb logcat");

所以我将使用 START 按钮启动记录器,以便开始记录 但我无法停止记录,因为 START 按钮在开始记录后总是被禁用并且无法选择 STOP 按钮。

通常在命令行中,我们可以使用 ctrl + c

终止 logcat

那么我如何在这种情况下终止 logcat 我尝试了process.destory()

我尝试使用 process.destroy() 但在启动和停止时创建文本文件以收集日志,但日志将继续在 Eclipse 的控制台中打印,因此进程不会被破坏,除非我关闭应用程序文本文件大小没有更新。

经过几次启动和停止

仍然会在 Eclipse 控制台中出现日志,

【问题讨论】:

  • 为什么 process.destory() 不起作用? process.destory() 将关闭这个进程
  • 是的,如果我在 START 按钮操作中写入它,它可以工作,但实际上我需要在用户单击 STOP 按钮时终止 logcat,所以在 START 我正在启动 logcat 并且同时启动按钮被禁用并且作为进程继续捕获无法单击 STOP 按钮的日志
  • 你的意思是你有两个按钮“START”,“STOP”,一旦点击“START”,启动adb,然后点击“STOP”停止adb?

标签: java process logcat terminate ctrl


【解决方案1】:

您的情况需要线程。

final Process exec = Runtime.getRuntime().exec("E:\\Android\\sdk\\platform-tools\\adb.exe logcat");

        //get the input stream
        InputStream inputStream = exec.getInputStream();
        InputStreamReader buInputStreamReader = new InputStreamReader(inputStream);
        final BufferedReader bufferedReader = new BufferedReader(buInputStreamReader);
        final StringBuilder buf = new StringBuilder();

        new Thread(){
            @Override
            public void run() {
                try {
                    String str = null;
                    while ((str = bufferedReader.readLine()) != null) {
                        buf.append(str);
                        buf.append("\n");
                        System.out.println(str);
                    }
                }catch (Exception ignore){}
            }
        }.start();

        JButton btnStop=new JButton("Stop");
        btnStop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exec.destroy();        
            }
        });

【讨论】:

  • 我尝试了上述解决方案,但是每次创建文本文件时我开始和停止的次数我已经添加了该部分但 exec.destory() 不破坏父进程它只会破坏子进程和当我将完全停止/杀死应用程序时,只有文件中的数据在更新。
  • 父进程是什么意思?调用 exec.destory() 后 adb.exe 应关闭
  • 是的停止它应该完全停止记录
  • adb.exe 应该关闭/停止,或者应该在停止按钮上释放文本文件
【解决方案2】:
String command = "ADB_PATH"+-s {deviceID} shell logcat -b crash;

      public LinkedHashSet<String> getAllDataFromCommand_v1(String command) {
                LinkedHashSet<String> resultSet = new LinkedHashSet<>();
                String s = null;
                try {
                    Process p =     Runtime.getRuntime().exec(command);
                    InputStreamReader in = new InputStreamReader(p.getInputStream());
                    BufferedReader stdInput = new BufferedReader(in);
                    BufferedReader stdError = new BufferedReader(new  InputStreamReader(p.getErrorStream()));
                    // read the output from the command
                    System.out.println("Here is the standard output of the command:\n"+command+"\n");
                    while (stdInput.ready()) {
                        s = stdInput.readLine();
                        resultSet.add(s);
                        System.out.println(s);
                    }
                    while (stdError.ready()) {
                        s = stdError.readLine();
                        System.out.println(s);
                        if(s.contains("not found")) {
                            System.out.println("Closing command");
                        }
                    }   
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                return resultSet;
            }

可以使用相同的代码来获取所有日志。它将捕获所有日志,直到输入可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多