標籤:
有時我們需要在程式執行進程中遇到一些異常,需要收集一logcat的資訊,android下就可以使用以下方法擷取:
private static String getLogcatInfo(){ String strLogcatInfo = ""; try{ ArrayList<String> commandLine = new ArrayList<String>(); commandLine.add( "logcat"); commandLine.add( "-d"); ArrayList<String> clearLog = new ArrayList<String>(); //設定命令 logcat -c 清除日誌 clearLog.add("logcat"); clearLog.add("-c"); Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[commandLine.size()])); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); String line = null; while ((line = bufferedReader.readLine()) != null) { Runtime.getRuntime().exec(clearLog.toArray(new String[clearLog.size()])); strLogcatInfo = strLogcatInfo + line + "\n"; } }catch(IOException e){ }catch(Exception ex){ } return strLogcatInfo; }
Android下讀取logcat的資訊