Android display Logcat in TextView

WorldWind picture WorldWind · Aug 17, 2014 · Viewed 20.9k times · Source

The app I'm building have root-access to Android. I wish to show Logcat log information in one of the TextView so it can show up on the screen while i'm debugging.

Can someone give me some ideas which library/function i can call to access those log?

Thanks

Answer

Dawid Czerwinski picture Dawid Czerwinski · Aug 17, 2014

Here's a blogpost that does exactly what you need it to do. It has a complete code example on how to display the contents of the Logcat log. Here's the code:

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

  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) {
              // Handle Exception
         }
    }

  } 

Just to clarify, this is not my answer, here is the original