要让程序获取root权限,前提是手机已经root了,一般情况下root过的手机都会有一个权限管理应用,像superSU、rootking等,当执行下列代码就会触发权限管理工具弹出提示框询问用户是否允许改应用获取root权限,获取root权限的应用才可以对系统文件进行修改。
获取root权限代码
public static boolean RootCommand(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)
{
Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
return false;
} finally
{
try
{
if (os != null)
{
os.close();
}
process.destroy();
} catch (Exception e)
{
}
}
Log.d("*** DEBUG ***", "Root SUC ");
return true;
}