0

検索したところ、次のコードを使用すると、プログラムが Android で 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 ;

    }   
4

1 に答える 1

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

次のような出力が得られます。

09-08 09:44:42.267 タグ付き (754): メッセージ
1 09-08 09:44:42.709 タグ付き (754): メッセージ 2
09-08 09:44:43.187 タグ付き (754): メッセージ
3 09- 08 09:44:45.295 E/タグ (754): message8

于 2012-04-20T16:09:04.280 に答える