STDOUT 到活动中的文本视图框



Hell0,我正在尝试将 shell 命令输出到文本视图框我收到返回丢失错误我想知道是否有人可以指出我正确的方向。我正在尝试构建一个运行几乎adb命令并将这些命令的结果输出到文本视图框的Android应用程序。

爪哇岛:

 public String runAsRoot() {
    try {
        // Executes the command.
        Process process = Runtime.getRuntime().exec("pm list packages");
        // 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();
        //I know im supposed to return output.toString();
        //but im trying to figure out how to return it to the textView.
        TextView tv = (TextView)findViewById(R.id.textView);
        tv.setText(output.toString());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
}

.XML:

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="431dp"
    android:layout_marginTop="60dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText"
    tools:layout_editor_absoluteX="0dp" />

很确定这对我来说是有效的!

protected void onCreate(Bundle savedInstanceState) {
    tv=(TextView)findViewById(R.id.textView);
    tv.setText("Output :"+"n"+runAsRoot());

最新更新