如何在Android应用程序中收集LogCat消息 [英] How to collect LogCat messages in an Android application

查看:778
本文介绍了如何在Android应用程序中收集LogCat消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想收集指定级别和标签的 LogCat 消息.

I have an application and I'd like to collect the LogCat messages of a specified level and tag.

我可以在某种程度上获取累积的消息吗?我不想一一收集信息,就像我使用adb读取实际日志时一样,它们应该是它们的总和.这可能吗?

Can I somehow get the accumulated messages at some point? I don't want to collect the messages one by one, it should be the sum of them like when I use adb to read the actual log. Is this possible?

推荐答案

请尝试以下操作:请注意,在Android 4中,除非您具有root访问权限,否则您只会看到自己的应用编写的日志消息.

Try this: Note that in Android 4 you will only see the log messages that were written by your own app unless you have root access.

public static String getLog(Context c) {
    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);
            log.append("\n");
        }

       return log.toString();

    } catch (IOException e) {
       return null;
    }
}

这篇关于如何在Android应用程序中收集LogCat消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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