android以编程方式清除日志

时间:2014-12-28 17:28:33

标签: android logcat

我想获得整个日志(Log.d(...)),按下按钮分析我们应用程序的某些部分(计算一些东西......)。我可以通过以下代码完成此操作:

HashMap<String, Integer> hashMapToSaveStuff = new HashMap<String, Integer>();
int count= 0;
String toCount= "";
try {
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (line.contains("MYSTRING")) {
                toCount = line.substring(line.indexOf(":") + 1);
                if (hashMapToSaveStuff.containsKey(toCount)) {
                    count = hashMapToSaveStuff.get(toCount);
                    count++;
                } else {
                    count= 1;
                }
                hashMapToSaveStuff.put(toCount, count);
            }
        }
    } catch (Exception e) {

    }

之后我会将结果发送到我们的服务器并将其保存在数据库中。因此,我想清除所有日志,我已经发送了。尝试使用以下代码执行此操作不起作用:

try {
        Process process = Runtime.getRuntime().exec("logcat -c");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);
        String line = bufferedReader.readLine();
    } catch (Exception e) {

    }

如何清除日志?

2 个答案:

答案 0 :(得分:5)

此代码过去对我有用:

Process process = new ProcessBuilder()
     .command("logcat", "-c")
     .redirectErrorStream(true)
     .start();

答案 1 :(得分:0)

此代码对我有用,可以放入@After case

流程流程=新流程构建器()      .command(&#34; logcat&#34;,&#34; -c&#34;)      .redirectErrorStream(真)      。开始();