How to read Logcat contents programmatically in Android?

By | July 16, 2011

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.

Leave a Reply

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