android以编程方式清除日志

android以编程方式清除日志,android,logcat,Android,Logcat,我想得到整个日志(log.d(…),在按下一个按钮来分析我们的应用程序的某些部分(计算一些东西…)之后。我可以通过以下代码执行此操作: HashMap<String, Integer> hashMapToSaveStuff = new HashMap<String, Integer>(); int count= 0; String toCount= ""; try { Process process = Runtime.getRuntime().exec(

我想得到整个日志(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) {

    }

如何清除日志?

此代码在过去对我有效:

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

此代码已正确地为我工作,并将其放入@After case中

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

我认为您需要以适当的权限运行以清除日志文件(或从您自己的进程中读取除日志以外的任何内容)。“shell”用户拥有必要的权限,这就是它从adb shell工作的原因。与其清除日志,不如应用时间戳过滤器,即使使用process.waitFor()也不行
     Process process = new ProcessBuilder()
        .command("logcat", "-c")
        .redirectErrorStream(true)
        .start();