本文实例讲述了Android手机获取root权限并实现关机重启功能的方法,是Android程序设计中非常常见的重要功能。现分享给大家,供大家在Android程序开发中参考之用。

具体功能代码如下:

/*
* 执行命令
* @param command
* 1、获取root权限 "chmod 777 "+getPackageCodePath()
* 2、关机 reboot -p
* 3、重启 reboot
*/
public static boolean execCmd(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
if(process != null) {
process.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}

希望本文所述实例对大家的Android程序设计起到一定的帮助作用。