android代码中获取logcat的方法实现
2015 年 4 月 7 日 android代码中获取logcat的方法实现无评论
我在手机客户端的项目中增加了错误上报的功能,当程序出现crash/崩溃时会弹出提示框,用户可以点击报告发送邮件到开发者。这篇文章仅描述抓取logcat的实现。
上报的具体实现可以参考
https://github.com/xdtianyu/SmallTalk/blob/master/app/src/main/java/org/xdty/smalltalk/activity/CrashDisplayActivity.java
方法实现
private String getSystemLog() {
Process process = null;
StringBuilder debugLog=new StringBuilder();
try {
process = Runtime.getRuntime().exec("logcat -d -v time");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
debugLog.append(line);
debugLog.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (process!=null)
process.destroy();
}
return debugLog.toString();
}
Process process = null;
StringBuilder debugLog=new StringBuilder();
try {
process = Runtime.getRuntime().exec("logcat -d -v time");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
debugLog.append(line);
debugLog.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (process!=null)
process.destroy();
}
return debugLog.toString();
}
另外需要在mainest中增加日志的权限
<uses-permission android:name="android.permission.READ_LOGS" />
Tags: android crash logcat 崩溃
发表评论