地狱0,我试图将shell命令输出到textView框我得到了一个返回丢失的错误我想知道是否有人可以指出我正确的方向。我正在尝试构建一个运行几乎adb命令的Android应用程序,并将这些命令的结果输出到textview框。
爪哇:
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" />
答案 0 :(得分:0)
非常确定这对我来说是正确的,因为它现在正在运作!
protected void onCreate(Bundle savedInstanceState) {
tv=(TextView)findViewById(R.id.textView);
tv.setText("Output :"+"\n"+runAsRoot());