Ausführen von shell-Befehlen, und bekommen Sie eine Ausgabe in einem TextView

Möchte ich ausführen, einige shell commands und erhalten die Ausgabe in eine TextView. Der Befehl kann über eine kontinuierliche Ausgabe wie ping oder logcat. Auch die TextView sollte automatisch scrollen, wie die Ausgabe des Befehls Hinzugefügt wird, in Echtzeit.
Um dies zu tun, habe ich dies getan:

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);
                }
            }
        }        
    }
}

Dies funktioniert gut, aber es aktualisiert nicht die TextView kontinuierlich. Wie Sie sehen können, bin ich der Ausführung radio-logcat als root-Benutzer, die Ausgabe, die generiert wird, kontinuierlich überprüft schon im terminal-emulator und adb-shell). Aber in meinem Fall, der Ausgang ist nicht kontinuierlich Hinzugefügt. Es Stoppt nach ein paar 100 Zeilen.
Hier ist mein layout:

    <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>

Den Ausgang zu halten, sollten Sie einen Bildlauf durch die TextView. Das ist zumindest das, was ich erwarte...... Abhilfe für dieses bitte?

InformationsquelleAutor Vinit Shandilya | 2014-05-12

Schreibe einen Kommentar