也许它与android有一些联系,但IMO并不多。
package com.main.app;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.TextView;
public class RootPermissionActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
outputStream.writeBytes("touchasd /m\n");
//codes below used to get the error output, but get nothing.
TextView suCommandMessage = (TextView)findViewById(R.id.suCommandMessage);
BufferedReader d = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String msg;
while((msg = d.readLine()) != null) {
suCommandMessage.setText(msg);
}
outputStream.writeBytes("exit \n");
p.waitFor();
} catch(IOException e) {
new AlertDialog.Builder(this)
.setTitle("Exception")
.setMessage("IOException: " + e)
.setPositiveButton("OK", null)
.show();
} catch(InterruptedException e) {
new AlertDialog.Builder(this)
.setTitle("Exception")
.setMessage("InterruptedException: " + e)
.setPositiveButton("OK", null)
.show();
}
}
}
首先我创建一个新进程su
以获得root权限,它只是一个运行shell命令的子进程,然后我将一些代码写入子进程,一切正常。然后我想获得子进程的错误输出,所以我写了一个错误的命令touchasd /m\n
,但我不知道如何获得子进程的错误输出,任何人都可以给我一个手?谢谢。 :)
答案 0 :(得分:0)
执行以下操作以获取流程的错误流。
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
我没有看到你的代码块中的注释,似乎上面的行和相关内容在你的代码块中被注释掉了。