私が自分のソフトウェア用に書いたこのコードをチェックしてください。それはうまくいきます。
Process p;
try {
// Try to execute SU
p = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
// User has responded.. Let's see whats the response.
// Execute ID to see if we are root
stdin.writeBytes("id\n");
DataInputStream stdout = new DataInputStream(p.getInputStream());
byte[] buffer = new byte[4096];
int read = 0;
String out = new String();
// Read the output
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<4096) break;
}
// Check output
if(!out.contains("root")){
// User denied the root prompt
Toast.makeText(getApplicationContext(), "Failed To Get Root! Are You Rooted!?", Toast.LENGTH_SHORT).show();
} else {
// Use granted... Do what you want here. Or set a flag that indicates we are now root
}
} catch (IOException e) {
e.printStackTrace();
}