Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式读取代码中的日志,并与android应用程序中的字符串匹配_Android_Logging_Event Handling - Fatal编程技术网

以编程方式读取代码中的日志,并与android应用程序中的字符串匹配

以编程方式读取代码中的日志,并与android应用程序中的字符串匹配,android,logging,event-handling,Android,Logging,Event Handling,我们如何以编程方式从android类读取日志(详细、调试等),然后在android中搜索字符串或与提供的字符串匹配 有时我们需要处理一些与系统或内核层相关的事件。但由于我们对这些代码的访问有限,我们无法处理它们。我们可以通过adb看到logcat,也可以看到一些来自内核/框架层的日志 现在的问题是,我们如何根据这些日志覆盖或处理应用程序中的某些事件?以下是任何android应用程序的解决方案: 我们需要用如下代码创建一个类: public class LogsUtil { private

我们如何以编程方式从android类读取日志(详细、调试等),然后在android中搜索字符串或与提供的字符串匹配

有时我们需要处理一些与系统或内核层相关的事件。但由于我们对这些代码的访问有限,我们无法处理它们。我们可以通过adb看到logcat,也可以看到一些来自内核/框架层的日志


现在的问题是,我们如何根据这些日志覆盖或处理应用程序中的某些事件?

以下是任何android应用程序的解决方案:

我们需要用如下代码创建一个类:

public class LogsUtil {
    private static final String processId = Integer.toString(android.os.Process
            .myPid());

    public static StringBuilder readLogs() {
        StringBuilder logBuilder = new StringBuilder();
        try {
            String[] command = new String[] { "logcat", "-d", "threadtime" };
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(processId)) {
                    logBuilder.append(line);
                    //Code here
                }
            }
        } catch (IOException e) {
        }
        return logBuilder;
    }
}
然后需要在我们的活动中编写以下代码,从中我们需要检查日志字符串:

//read the logs    
StringBuilder logs = LogsUtil.readLogs();

    if(logs.toString().contains("your_text"))
        //your code
    else //your code