我试图在我的应用程序中要求root访问权限,但后台线程卡在waitFor()中; 我做错了什么?
try {
Log.i("DEBUG", "start");
Runtime.getRuntime().exec("su").waitFor();
Log.i("DEBUG", "yay");
..........
} catch (InterruptedException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
查看我为自己的软件编写的代码。效果很好。
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();
}