2012-06-14 84 views
1

我想顯示應用程序日誌。 在終端上我使用了這個命令:adb logcat -s brief *:V | grep「pid」 它顯示我的應用程序日誌。在android中執行管道命令?

pid表示在logcat表中顯示的應用程序pid。

public static String logProc() 
{ 
    String value = ""; 
    try 
    { 
     String cmd[] = {"logcat","-s","brief","*:V","|","grep", 
       android.os.Process.myPid()+""}; 
     Process p = Runtime.getRuntime().exec(cmd,null, null); 
     BufferedReader reader = new BufferedReader(new 
       InputStreamReader(p.getInputStream())); 
     String line = reader.readLine(); 

     while (line != null) 
     { 
      value += line + "\n"; 
      line = reader.readLine(); 
     } 
     p.waitFor(); 
    } 
    catch (IOException e1) 
    { 
     e1.printStackTrace(); 
    } 
    catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return value; 
} 
+1

這是什麼問題? –

+0

@Deepali:問題是它沒有顯示任何輸出。它的連續顯示如下日誌06-14 15:16:43.104:I/dalvikvm-heap(591):爲881344字節的分配增加堆(碎片情況)到4.535MB 06-14 15:16:43.154:D/dalvikvm(591):GC_FOR_MALLOC在37ms中釋放了3個對象/ 587632字節 – Deepali

+0

如何以編程方式在android中執行「adb logcat -s brief *:V | grep」pid「」命令 – Deepali

回答

1

大多數官方Android版本不前來使用grep(編輯:更新的版本現在做的)

,如果你想額外的命令,你可以安裝busybox的 - 沒有根,你就必須把它放在一個備用位置。

您還有一個額外的問題,即您嘗試執行exec()shell命令以通過管道連接兩個程序,除非您執行shell解釋器併爲其提供該命令,否則這不會起作用。或者你可以自己設置管道並執行這兩個程序。

但是,由於您正在編寫程序,因此在java代碼中進行模式匹配可能會更簡單。