为什么我不能看我在程序的logcat的输出? [英] why can't I read the logcat output in my program?

查看:188
本文介绍了为什么我不能看我在程序的logcat的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜查,我发现以下codeS让我的程序读取android.However的logcat的输出,在我peroidically调用此函数,没有什么happens.Nothing通过System.out的输出除了logcat中被称为。我真的不知道发生了什么事,因为很多帖子在这里告诉这将工作:LT;

 公共无效Collector_logcat(){
    字符串的StringBuffer =;
    字符串命令=的logcat -d
    字符串command_c =logcat的-c
     的System.out.println(logcat中称为\\ n);
    尝试{
        m_logcatprocess =调用Runtime.getRuntime()EXEC(命令)。
        m_logcat_inputreader =新的InputStreamReader(m_logcatprocess.getInputStream());
        m_logcat_reader =新的BufferedReader(m_logcat_inputreader);
      而((StringBuffer的= m_logcat_reader.readLine())!= NULL){
          的System.out.println(StringBuffer的+\\ n);
      }      调用Runtime.getRuntime()EXEC(command_c)。    }        赶上(例外前){
            的System.out.println(ex.getMessage());
            的System.out.println(错误Collector_logcat \\ n);
        }     返回;    }


解决方案

试试这个:

的Andr​​oidManifest.xml

 <使用许可权的android:NAME =android.permission.READ_LOGS/>

获取目录下载:

  {尝试
    ArrayList的<串GT;命令行=新的ArrayList<串GT;();
commandLine.add(logcat的);
    commandLine.add(-d);
    commandLine.add(-v);
    commandLine.add(时代);
    commandLine.add(-s);
    commandLine.add(标记:W);
    工艺过程=调用Runtime.getRuntime()EXEC(commandLine.toArray(新的String [commandLine.size()))。
    的BufferedReader的BufferedReader =新的BufferedReader(新的InputStreamReader(process.getInputStream()),1024);
    串线;
    而((行= bufferedReader.readLine())!= NULL){
        log.append(线);
        log.append(\\ n)
    }
}赶上(IOException异常五){
}

您将得到作为输出:

九月九日至8日:44:42.267 W /标签(754):MESSAGE1结果
9月9日至八日:44:42.709 W /标签(754):MESSAGE2结果
9月9日至八日:44:43.187 W /标签(754):MESSAGE3结果
9月9日至八日:44:45.295 E /标签(754):message8

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 ;

    }   

解决方案

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

这篇关于为什么我不能看我在程序的logcat的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆