如何在Android中读取系统日志文件?

Jim*_*mmy 5 android

我试图在我的代码中读取系统日志以生成类似错误报告的内容.与adb logcat类似,但是采用编程方式.

我怎样才能做到这一点?

Ebo*_*ike 6

Log Collector在Google Code上提供了源代码.他们实际上只是调用logcat.请看这里:android-log-collector - SendLogActivity.java

这是关键部分:

ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");//$NON-NLS-1$
commandLine.add("-d");//$NON-NLS-1$
ArrayList<String> arguments = ((params != null) && (params.length > 0)) ? params[0] : null;

if (null != arguments){
    commandLine.addAll(arguments);
}

Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;
while ((line = bufferedReader.readLine()) != null){ 
     log.append(line);
     log.append(App.LINE_SEPARATOR); 
}
Run Code Online (Sandbox Code Playgroud)