android clear log programmatically

偶尔善良 提交于 2019-12-09 00:55:00

问题


I want to get the whole log (Log.d(...)), after pressing a button to analyse some parts of our app (count something...). I'm able to do this by the following code:

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) {

    }

After that I'll send the result to our server and save it on database. Because of that I want to clear all the logs, I've already send. Trying to do this with the following code didn't work:

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

    }

How can I clear the log?


回答1:


This code has worked for me in the past:

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



回答2:


This code has worked for me correctly which is put into @After case

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


来源:https://stackoverflow.com/questions/27678829/android-clear-log-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!