我有一个应用程序,我想收集指定级别和标记的LogCat消息.
我能以某种方式获得累积的消息吗?我不想逐个收集消息,它应该是它们的总和,就像我使用adb来读取实际日志一样.这可能吗?
试试这个:请注意,在Android 4中,您只会看到由您自己的应用编写的日志消息,除非您具有root访问权限.
public static String getLog(Context c) {
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
return log.toString();
} catch (IOException e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)