title: Execute shell commands and get output in a TextView
body: <p>I want to execute some <code>shell commands</code> and get the output in a <code>TextView</code>. The command may have a continuous output like ping or <code>logcat</code>. Also, the <code>TextView</code> should scroll automatically as the command output is added in real-time.
  In order to do so, I have done this:     </p>
  
  <pre><code>package com.example.rootapp;
  
  import java.io.DataOutputStream;
  import java.io.InputStream;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.text.method.ScrollingMovementMethod;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
      TextView tv;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          String[] cmdArray={"logcat -b radio"};
          try {
              runAsRoot(cmdArray);
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public void runAsRoot(String[] cmds) throws Exception {
          tv=(TextView)findViewById(R.id.cmdOp);
          tv.setMovementMethod(new ScrollingMovementMethod());
          Process p = Runtime.getRuntime().exec("su");
          DataOutputStream os = new DataOutputStream(p.getOutputStream());
          InputStream is = p.getInputStream();
          for (String tmpCmd : cmds) {
              os.writeBytes(tmpCmd+"\n");
              int readed = 0;
              byte[] buff = new byte[4096];
              boolean cmdRequiresAnOutput = true;
              if (cmdRequiresAnOutput) {
                  while( is.available() &lt;= 0) {
                      try { Thread.sleep(5000); } catch(Exception ex) {}
                  }
  
                  while( is.available() &gt; 0) {
                      readed = is.read(buff);
                      if ( readed &lt;= 0 ) break;
                      String seg = new String(buff,0,readed);   
                      tv.append(seg);
                  }
              }
          }        
      }
  }
  </code></pre>
  
  <p>This works fine, but it does not update the <code>TextView</code> continuously. As you can see, I'm executing radio logcat as root user, the output is generated continuously (already checked in terminal emulator and adb shell). But in my case, the output is not added continuously. It stops after a few 100 lines.
  Here is my layout:</p>
  
  <pre><code>    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;TextView
          android:id="@+id/cmdOp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="bottom"
          android:text="@string/hello_world" /&gt;
  &lt;/ScrollView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The output should keep scrolling the TextView. At least this is what I expect...... Any workaround for this please?</p>