执行shell命令并在TextView中获取输出

时间:2014-05-12 11:39:09

标签: android shell scrollview android-logcat su

我想执行一些shell commands并在TextView中获取输出。该命令可能具有ping或logcat等连续输出。此外,TextView应在实时添加命令输出时自动滚动。 为了做到这一点,我做到了这一点:

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

这样可以正常工作,但它不会持续更新TextView。如您所见,我正在以root用户身份执行radio logcat,输出会不断生成(已经在终端模拟器和adb shell中检查过)。但在我的情况下,输出不会连续添加。它在几百行后停止。 这是我的布局:

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

输出应该继续滚动TextView。至少这是我的期望......请为此解决这个问题吗?

3 个答案:

答案 0 :(得分:9)

您可以将命令和显示命令输出运行到文本中,如下所示:

public class MainActivity extends Activity {

    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.cmdOp);
        tv.setText("Output :"+"\n"+runAsRoot());
    }

    public String runAsRoot() {

        try {
            // Executes the command.
            Process process = Runtime.getRuntime().exec("ls -l");

            // Reads stdout.
            // NOTE: You can write to stdin of the command using
            //       process.getOutputStream().
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();

            // Waits for the command to finish.
            process.waitFor();

            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

注意:&#34; su&#34;命令仅在您的设备以root为根的情况下运行,否则会给您例外。

答案 1 :(得分:1)

我认为问题是你等待5秒开始读取输出,当程序启动时它到达流的末尾,因为它比生成日志的速度快。因此,您需要再等一段时间,然后再次开始读取缓冲区。以下代码适用于我。我使用ArrayAdapter + ListView而不是TextView。如果我们将m_bTerminate设置为true,那么线程在下次到达输入流的末尾时就会死掉。

Process p = Runtime.getRuntime().exec(m_command);
InputStream is = p.getInputStream();

while(!m_bTerminate) {
    while(is.available() <= 0)
    {
        try{ Thread.sleep(1000, 0); } catch(Exception e) {m_log.e(e);}
    }

    byte[] buff = new byte[4096];
    int read = is.read(buff);
    if(0 < read) {
        m_List.add(new String(buff, 0, read));
        m_handler.sendEmptyMessage(Handler.MSG_RADIO_LOG_NOTIFY_DATASET_CHANGED);
        //if we call notify datasetchanged directly it raises
        //android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views
        //m_Adapter.notifyDataSetChanged();
    }
}

m_log.p(TAG, Log.DEBUG, "Command executed. Destroying thread.");
is.close();
p.destroy();

答案 2 :(得分:0)

采用此 String 函数,并将结果存储到TextView或所需的任何位置

public String shell_exec(String cmd)
     {
     String o=null;
     try
       {
       Process p=Runtime.getRuntime().exec(cmd);
       BufferedReader b=new BufferedReader(new InputStreamReader(p.getInputStream()));
       String line;
       while((line=b.readLine())!=null)o+=line;
       }catch(Exception e){o="error";}
     return o;
     }

用法示例:

mytextview.setText(shell_exec("ls -l"));