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

£可爱£侵袭症+ 提交于 2019-12-23 04:36:15

问题


I have searched and I have found the following codes will let my program read the output of the logcat in android.However,after I call this function peroidically, nothing happens.Nothing is output through system.out except the "logcat called".I really do not know what happened because many posts here tell this will work:<

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 ;

    }   

回答1:


try this:

AndroidManifest.xml

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

Get catlog:

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) {  
}

You will get output as:

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



来源:https://stackoverflow.com/questions/10249207/why-cant-i-read-the-logcat-output-in-my-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!