我想获得整个日志(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) {
}
我该如何清除日志?
解决方法:
这段代码过去对我有用:
Process process = new ProcessBuilder()
.command("logcat", "-c")
.redirectErrorStream(true)
.start();