简体   繁体   English

为什么我不能在程序中读取logcat输出?

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

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:< 我进行了搜索,发现以下代码将使我的程序在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 ;

    }   

try this: 尝试这个:

AndroidManifest.xml 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.267 W /标签(754):message1
09-08 09:44:42.709 W/tag ( 754): message2 09-08 09:44:42.709 W /标签(754):message2
09-08 09:44:43.187 W/tag ( 754): message3 09-08 09:44:43.187 W /标签(754):message3
09-08 09:44:45.295 E/tag ( 754): message8 09-08 09:44:45.295 E / tag(754):message8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM