我想读取 Android 系统级日志文件。所以我使用以下代码:
Process mLogcatProc = null;
BufferedReader reader = null;
try {
mLogcatProc = Runtime.getRuntime().exec(
new String[] { "logcat", "-d",
"AndroidRuntime:E [Your Log Tag Here]:V *:S" });
reader = new BufferedReader(new InputStreamReader(mLogcatProc
.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
log.append(line);
log.append(separator);
}
}
catch (IOException e) {}
finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {}
}
我也在 AndroidManifest.xml 中使用过。但我看不懂任何一行。StringBuilder 日志为空。并且 mLogcatProc.waitFor 方法返回 0。
那么如何阅读日志呢?