ssummeraw 2013-01-08 03:28 采纳率: 10%
浏览 3955
已采纳

为什么不能读出程序中的logcat 的输出?

我查的资料以下的代码可以从程序中读出logcat 的输出信息。然而,当我调用这个函数时,并没起作用,从system.out中除了输出"logcat called"以外,再没有输出别的任何信息。资料中说的这段代码没错,但是现在实现不了。

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 ;

    } 
  • 写回答

2条回答

  • yongyong_21 2013-01-08 06:19
    关注

    试一下:
    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 = bufferedReader.readLine();  
        while ( line != null) {  
            log.append(line);  
            log.append("\n")  
        }  
    } catch ( IOException e) {  
    }
    

    你将会得到以下的输出:
    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用