This is an example to read Logcat contents programmatically in Android.
Normal developers won’t need this, but still it is a good thing to know how to do it.

package pack.coderzheaven;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ReadLogDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
    	 setContentView(R.layout.main);
    	    try {
    	      Process process = Runtime.getRuntime().exec("logcat -d");
    	      BufferedReader bufferedReader = new BufferedReader(
    	      new InputStreamReader(process.getInputStream()));

    	      StringBuilder log=new StringBuilder();
    	      String line = "";
    	      while ((line = bufferedReader.readLine()) != null) {
    	        log.append(line);
    	      }
    	      TextView tv = (TextView)findViewById(R.id.textView1);
    	      tv.setText(log.toString());
    	    } catch (IOException e) {
    	    }
    	  }
}
Read Logcat Demo

Read Logcat Demo

if you like the post then click on the plus button to share it with your friends and leave your valuable comments.

 

6 Comments

  1. murali September 18, 2011 at 11:24 am

    I have run the sample application. it runs successfully. but i use “locat -e” option instead of “logcat -d”. it does not print anything in the text view. i requested you to add the solution for it.

    Thank you.

     
    • naveen January 25, 2012 at 5:34 pm

      u need to add
      in manifest file

       
  2. Tauon August 22, 2012 at 8:07 am

    Unfortunately, a cut and paste of this code does not work. The very first readLine() returns null and so it drops straight out the loop. I am guessing that because Murali got it working, there is something that needs to be done that has not been described in this post?

     
  3. Kontakthi September 17, 2012 at 6:44 pm

    Sorry,a bit late but I resolved the null return from the readLine() call by adding the following line to my manifest file:

     
    • Arun Pandian August 8, 2013 at 1:21 pm

      Hi,
      If u r wondering, set your permission in android manifest xml page to

      ..

      Please let me your solutions

       
      • James August 15, 2013 at 1:28 pm

        No need to add any permissions.

         

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.