有很多apk在启动的时候会弹出一个授权请求,而得到系统的一些权限,在此处将其代码实现写出,其实很简单的看一遍就会
1:在你程序的第一个Activity的onCreate中加:
getRootAhth();
2:实现
/*
* 取得root权限
*
* @return true 有root权限 false 没有root权限
*/
public static synchronized boolean getRootAhth() {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("exit\n");
os.flush();
int exitValue = process.waitFor();
if (exitValue == 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "
+ e.getMessage());
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
}
OK!