簡體   English   中英

為什么我不能在程序中讀取logcat輸出?

[英]why can't I read the logcat output in my program?

我進行了搜索,發現以下代碼將使我的程序在android中讀取logcat的輸出。但是,在我周期性地調用此函數之后,什么也沒有發生。除了“ logcat被調用”之外,沒有通過system.out輸出任何內容。真的不知道發生了什么,因為這里的許多帖子都說這會起作用:

public void  Collector_logcat(){


    String stringbuffer="";
    String command="logcat -d";
    String command_c="logcat -c";
     System.out.println("logcat called\n"); 
    try{
        m_logcatprocess=Runtime.getRuntime().exec(command);
        m_logcat_inputreader=new InputStreamReader(m_logcatprocess.getInputStream());
        m_logcat_reader=new BufferedReader(m_logcat_inputreader);
      while((stringbuffer=m_logcat_reader.readLine())!=null){
          System.out.println(stringbuffer+"\n");  
      }

      Runtime.getRuntime().exec(command_c);

    }

        catch(Exception ex){
            System.out.println(ex.getMessage());
            System.out.println("error in Collector_logcat\n");
        }

     return ;

    }   

嘗試這個:

AndroidManifest.xml中

<uses-permission android:name="android.permission.READ_LOGS" />

獲取目錄:

try {  
    ArrayList<String> commandLine = new ArrayList<String>();  
commandLine.add("logcat");  
    commandLine.add( "-d");  
    commandLine.add( "-v");  
    commandLine.add( "time");  
    commandLine.add( "-s");  
    commandLine.add( "tag:W");  
    Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[commandLine.size()]));  
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);  
    String line;  
    while ((line = bufferedReader.readLine()) != null) {  
        log.append(line);  
        log.append("\n")  
    }  
} catch (IOException e) {  
}

您將獲得以下輸出:

09-08 09:44:42.267 W /標簽(754):message1
09-08 09:44:42.709 W /標簽(754):message2
09-08 09:44:43.187 W /標簽(754):message3
09-08 09:44:45.295 E / tag(754):message8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM