Exécuter des commandes shell et obtenir une sortie dans un TextView

Je veux exécuter certaines shell commands et obtenir la sortie dans un TextView. La commande peut avoir une puissance continue comme le ping ou logcat. Aussi, le TextView défiler automatiquement la sortie de la commande est ajoutée en temps réel.
Pour ce faire, j'ai fait ceci:

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() <= 0) {
try { Thread.sleep(5000); } catch(Exception ex) {}
}
while( is.available() > 0) {
readed = is.read(buff);
if ( readed <= 0 ) break;
String seg = new String(buff,0,readed);   
tv.append(seg);
}
}
}        
}
}

Cela fonctionne bien, mais il ne fait pas de mise à jour de la TextView en permanence. Comme vous pouvez le voir, je suis de l'exécution de la radio logcat en tant qu'utilisateur root, la sortie est généré en continu (déjà vérifié dans l'émulateur de terminal et la commande adb shell). Mais dans mon cas, la sortie n'est pas ajouté en continu. Il s'arrête au bout de quelques 100 lignes.
Voici ma présentation:

    <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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/cmdOp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="@string/hello_world" />
</ScrollView>
</RelativeLayout>

La sortie doit se garder de faire défiler la TextView. Au moins ce est ce que j'attends...... Aucune solution de contournement pour ce s'il vous plaît?

OriginalL'auteur Vinit Shandilya | 2014-05-12