2012-04-20 71 views
0

我已经搜索了,我发现下面的代码会让我的程序读取android中logcat的输出。但是,在我调用这个函数之后,没有任何反应。没有任何东西通过system.out输出,除了“logcat called” 。我真的不知道发生了什么,因为很多帖子在这里告诉这将工作:<为什么我不能在程序中读取logcat输出?

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 ; 

    } 
+0

如果我尝试直接读取/ dev/log/main文件,它显示permis锡永被拒绝。我不希望我的程序获得root previlige – 2012-04-20 15:38:00

+1

logcat在停止工作时是否出现错误? – dymmeh 2012-04-20 15:41:35

+0

@dymmeh没有任何错误。唯一的输出是“logcat called”.... – 2012-04-20 15:44:28

回答

5

试试这个:

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

你会得到输出:

9月9日至8日:44:42.267 W /标签(754):MESSAGE1
9月9日至8日:44:42.709 w^/标签(754):消息2
9月9日至8日:44:43.187 W /标签(754):消息3
9月9日至8日:44:45.295 E /标签(754):message8

+1

...哦,太棒了。重点是我忘记了xml文件的权限。我讨厌android,好几次我花了很多时间试图调试这个程序,最后我发现它是权限。谢谢... – 2012-04-20 16:13:42

相关问题