在应用程序中以编程方式读取 logcat [英] Read logcat programmatically within application

查看:45
本文介绍了在应用程序中以编程方式读取 logcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中读取 logcat 日志并对其做出反应.

I want to read and react to logcat logs within my application.

我找到了以下代码:

try {
  Process process = Runtime.getRuntime().exec("logcat -d");
  BufferedReader bufferedReader = new BufferedReader(
  new InputStreamReader(process.getInputStream()));

  StringBuilder log=new StringBuilder();
  String line = "";
  while ((line = bufferedReader.readLine()) != null) {
    log.append(line);
  }
  TextView tv = (TextView)findViewById(R.id.textView1);
  tv.setText(log.toString());
  } 
catch (IOException e) {}

这段代码确实返回了在应用程序启动之前制作的 logcat 日志 -

This code indeed returns the logcat logs that made until the application was started -

但是是否有可能连续收听新的 logcat 日志?

But is it possible to continuously listen to even new logcat logs?

推荐答案

您可以继续阅读日志,只需删除上面代码中的-d"标志即可.

You can keep reading the logs, just by removing the "-d" flag in your code above.

-d"标志指示 logcat 显示日志内容并退出.如果您删除该标志,logcat 将不会终止并继续发送任何添加到其中的新行.

The "-d" flag instruct to logcat to show log content and exit. If you remove the flag, logcat will not terminate and keeps sending any new line added to it.

请记住,如果设计不正确,这可能会阻止您的应用程序.

Just have in mind that this may block your application if not correctly designed.

祝你好运.

这篇关于在应用程序中以编程方式读取 logcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆