Android下讀取logcat的信息


有時我們需要在程序執行進程中遇到一些異常,需要收集一logcat的信息,android下就可以使用以下方法獲取:

private static String getLogcatInfo(){
        String strLogcatInfo = "";        
        try{
            ArrayList<String> commandLine = new ArrayList<String>();
            commandLine.add("logcat");   
            commandLine.add( "-d"); 
commandLine.add("*:E"); // 過濾所有的錯誤信息
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"; }

bufferedReader.close(); }
  
catch(Exception ex)
{
process.destroy(); }
return strLogcatInfo; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM