系统reboot命令有两个功能(我暂时知道的),关机跟重启,一开始以为它就只会用来重启的,下面用java代码调用这个命令来实现关机和重启
public static int shutdown() {
int r = 0;
try {
Process process = Runtime.getRuntime().exec(new String[]{"su" , "-c" ,"reboot -p"});
r = process.waitFor();
java.lang.System.out.println("r:" + r );
} catch (IOException e) {
e.printStackTrace();
r = -1;
} catch (InterruptedException e) {
e.printStackTrace();
r = -1;
}
return r;
}
public static int reboot() {
int r = 0;
try {
Process process = Runtime.getRuntime().exec("su -c reboot");
r = process.waitFor();
java.lang.System.out.println("r:" + r );
} catch (IOException e) {
e.printStackTrace();
r = -1;
} catch (InterruptedException e) {
e.printStackTrace();
r = -1;
}
return r;
}
说明:su -c 是使用超级用户执行某一命令
关于exec(new String[]{"su" , "-c" , "reboot -p"}); 换成exec(“su -c reboot -p”);,感觉系统忽略了-p参数,-c 后面跟的“reboot -p”应该算作一条命令,如果"su -c reboot -p"的话,"-p" 就当作是su的一个参数了