0

我试图在我的应用程序中要求 root 访问权限,但后台线程卡在 waitFor(); 我究竟做错了什么?

        try {
            Log.i("DEBUG", "start");
            Runtime.getRuntime().exec("su").waitFor();
            Log.i("DEBUG", "yay");

            ..........
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
4

1 回答 1

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();
    }
于 2013-06-16T17:51:26.310 回答