在应用程序读取logcat的编程应用程序、logcat

2023-09-12 00:27:23 作者:吴腾龙

我想读并作出反应在我的应用程序的logcat日志。

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

我发现旁边code:

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) {}

这code确实返回,使得直到应用程序的当前运行的logcat的日志 -

This code indeed returns the logcat logs that made until the current run of the application -

但有可能为在线听logcat的日志?

But is it possible to "online" listen to logcat logs?

推荐答案

您可以继续阅读日志,只是去掉了-d标志,在code以上。

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.

好运。