2017-03-17 98 views
0

我想單獨獲取我放入應用程序中的日誌條目並將其顯示在可滾動的textview中。例如android如何以編程方式僅捕獲應用程序中的我的日誌條目

Log.e(SAMPLE_ACTIVITY_TAG, "App started") 

我需要那些錯誤日誌,我不想要其他不需要的日誌。 logcat命令行工具有可能嗎?我曾嘗試下面的代碼,但我得到完整的應用設備日誌

Process process = Runtime.getRuntime().exec("logcat -d"); 
     BufferedReader bufferedReader = new BufferedReader(
       new InputStreamReader(process.getInputStream())); 

     StringBuilder log=new StringBuilder(); 
     String line = ""; 
     while ((line = bufferedReader.readLine()) != null) { 
      log.append(line +"\n"); 
     } 
     textView.setText(log.toString()); 
     textView.setMovementMethod(new ScrollingMovementMethod()); 

任何建議

回答

0

請與下面給出的命令創建進程對象:

logcat的--pid = PID -d

您需要用您的應用程序進程ID替換PID。進程ID可以使用下面的代碼行

int pid = android.os.Process.myPid(); 

你最終的源代碼應該是這樣的獲取。請試一試

 int pid = android.os.Process.myPid(); 
     String command = "logcat --pid="+pid+" -d"; 
     Process process = Runtime.getRuntime().exec(command); 
     BufferedReader bufferedReader = new BufferedReader(
       new InputStreamReader(process.getInputStream())); 

     StringBuilder log=new StringBuilder(); 
     String line = ""; 
     while ((line = bufferedReader.readLine()) != null) { 
      log.append(line +"\n"); 
     } 
     textView.setText(log.toString()); 
     textView.setMovementMethod(new ScrollingMovementMethod()); 
相關問題