无法在我的Android应用程序中打印日志猫

时间:2011-08-27 10:37:57

标签: android android-logcat

我正在为文本视图中的打印日志cat信息做一个应用程序。我使用以下代码来做到这一点。

Log.e("msg1","message1");
Log.e("msg2","message2");
Log.e("msg3","message3");
try {
         String separator = System.getProperty("line.separator"); 
                 try {
                     Process mProcess = Runtime.getRuntime().exec("logcat -e");
                     BufferedReader reader =  new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
                     StringBuilder builder = new StringBuilder();
                     String line = "";

                     while ((line = reader.readLine())!= null) {
                         builder.append(line);
                         builder.append(separator);
                     }
                     System.out.println(separator +"OUTPUT - "+builder.toString());
                     tv.setText(builder.toString());
    } catch (IOException e) { }

并在清单文件中我给出了如下许可:

      <uses-permission android:name="android.permission.READ_LOGS" />

我无法从文本视图中的locat获取任何信息。 这是eclipse logcat中代码的输出:

09-18 11:01:51.649: INFO/ActivityManager(66): Displayed activity com.log.cat/.main1: 2224 ms (total 2224 ms)
09-18 11:01:52.799: INFO/ActivityManager(66): Starting activity: Intent { cmp=com.log.cat/.main (has extras) }
09-18 11:01:52.958: ERROR/msg1(346): message1
09-18 11:01:52.958: ERROR/msg2(346): message2
09-18 11:01:52.969: ERROR/msg3(346): message3
09-18 11:01:53.028: INFO/global(346): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
09-18 11:01:53.059: INFO/System.out(346): OUTPUT - 
09-18 11:01:53.588: INFO/ActivityManager(66): Displayed activity com.log.cat/.main: 754 ms (total 754 ms)

system.out。不会在logcat中打印任何东西。如果我使用 logcat -d 而不是 logcat -e 。它打印所有系统相关信息,因为没有我的logcat信息。请帮我。我需要在textview中显示我的app logcat信息而不是其他信息,以便我使用 logcat -e 。请帮帮我。

1 个答案:

答案 0 :(得分:0)

这是我的工作代码 您正在使用Runtime.getRuntime().exec("logcat -e"); 因此,如果有任何错误,它会给你 因此,如果执行中没有任何错误,则不会在textview中显示任何文本

如果您要测试代码,请将值Runtime.getRuntime().exec("logcat -e");更改为Runtime.getRuntime().exec("logcat -d");

您将在文本视图中获取文字

有V,D,I,W,E五种类型的logcat类别

有关详情,请参阅以下链接

Click here

相关问题