百度到的,忘记出处了。
这么做的前提是手机rom已经是root过的。
pkgPath = getPackageCodePath();
private boolean requestRootPermission(String pkgPath)
{
Process process = null;
DataOutputStream os = null;
try {
String cmd="chmod 777 " + pkgPath;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
} 试验成功。看代码就明白原理了,就相当于执行了一句linux的chmod命令。