我已经搜索过,我发现下面的代码会让我的程序读取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 ;
}
答案 0 :(得分:5)
试试这个:
<强>的AndroidManifest.xml 强>
<uses-permission android:name="android.permission.READ_LOGS" />
获取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) {
}
您将获得输出:
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