如何使用Runtime.getRuntime()。exec更改文件的权限

时间:2012-03-29 14:45:30

标签: android-ndk

我在android上编写代码,我需要更改某些文件的权限,我已经安装了Busybox以获得许多命令,但是当执行代码时什么也没做。

Process p = Runtime.getRuntime().exec("chmod 777 /data/app/XXX"); 
BufferedReader in = new BufferedReader(  
                                new InputStreamReader(p.getInputStream()));  
String line = null;  
while ((line = in.readLine()) != null) {  
                System.out.println(line);
}

1 个答案:

答案 0 :(得分:0)

private boolean isSu() throws IOException, InterruptedException {
Process p;
    try {
    // Preform su to get root privledges
    p = Runtime.getRuntime().exec("su");

    // Attempt to write a file to a root-only
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");

        // Close the terminal
        os.writeBytes("exit\n");
    os.flush();
    try {
    p.waitFor();
    if (p.exitValue() != 255) {
        toastMessage("root");
        return true;
        } else {
        toastMessage("not root");
    }
    } catch (InterruptedException e) {
        toastMessage("not root");
        }
    } catch (IOException e) {
        toastMessage("not root");
    }

    return false;
}