如何根据查找的文本扫描logcat输出和发布事件? [英] How do you scan logcat output and post event based on found text?

查看:156
本文介绍了如何根据查找的文本扫描logcat输出和发布事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我找到我想要的文本,我想扫描logcat活动和post和event。我正尝试在这里:

I would like to scan logcat activity and post and event if I find my desired text. I am trying to approach here:

记录logcat活动

但我想知道如何对日志中发现的内容做出反应,例如作为发布事件,所以另一个对象可以处理它。

but I would like to know how to react to what I find in the log, such as posting an event so another object can handle it.

推荐答案

获取所提到的答案中发布的所有日志消息的字符串。然后,使用正则表达式搜索字符串。

Get the string of all log messages as posted in the answer you mentioned. Then, simply search the string using regular expressions.

例如,

String result = //... get the logs ...

if(result.contains("trigger text i am looking for")){
  myObject.raiseEvent();
}

编辑

如果您试图对logcat进行持续监控,这将会更加困难。一个,这个过程可能会在没有警告的情况下关闭。所以您需要一种方法来保持运行状态,或者不断检查它是否正在运行。

If you are trying to do a constant monitoring of the logcat, this will be much more difficult. For one, the process might get shut down without warning. So you need a way to keep it running, or to constantly check that it is running.

其次,您链接的解决方案在这种情况下将不起作用,因为等待直到stopLogging被调用,然后返回记录间隔的整个日志。

Secondly, the solution you linked to would not work in this case, as that waits until stopLogging is called, then returns the entire log for the recorded interval.

此外,您必须修改该代码,使其具有触发词列表和他们相关联的回调函数运行。

Also, you'll have to modify that code so that it has a list of trigger words and their associated callback functions to run.

while ((line = reader.readLine()) != null){
 for(int i =0; i < triggerList.size(); i++){
   TriggerPhrase trigger = triggerList.get(i);
   if(line.contains(trigger.phrase))
   {
     trigger.onTriggerPhrase(line);
   }
 } 
}

其中TriggerPhrase是一个简单的类它有一个String短语成员变量和一个实现回调函数的对象。

where TriggerPhrase is a simple class that has a String phrase member variable and an object that implements a callback function.

public static class TriggerPhrase implements TriggerListener{
  String phrase;
  TriggerListener callback;
}

public interface TriggerListener{
   public void onTriggerPhrase();
}

然后在开始侦听日志之前,您填充triggerPhrases List 在LogCat监听器中。

Then before you start listening the logs, you populate the triggerPhrases List in the LogCat listener.

TriggerPhrase monkeyPhrase = new TriggerPhrase();
monkeyPhrase.phrase = "monkeys";
monkeyPhrase.callback = new TriggerListener(){
  public void onTriggerPhrase(){
    notifyUser("found the phrase `monkeys` in the logcat");
  }
};
triggerPhrases.add(monkeyPhrase);
//etc.

这篇关于如何根据查找的文本扫描logcat输出和发布事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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