Android clear log programmatically

I want to get the whole log (Log.d (...)) by clicking a button to analyze some parts of our application (count something ...). I can do it with 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 will send the result to our server and save it to the database. Because of this, I want to clear all the logs that I have already submitted. Trying to do it 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?

+4


source to share


2 answers


This code has worked for me in the past:



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

      

+5


source


This code worked for me correctly which is placed in the @After case



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

      

0


source







All Articles