Android root 权限
What
root权限就是Android系统管理员用户所拥有的权限,它可以访问和修改手机几乎所有的文件,只有root才具备最高级别的管理权限。
Why
掌控手机系统的全新,可以做一下超级用户才会做的事情,比如删除系统软件,更换开关机铃声与动画,拦截状态栏弹出的广告等等…
How
如何判断手机是否存在root权限
1.根据手机对应目录下是否存在su来判断手机是否root。
public boolean isRoot(){
if (systemRootState == kSystemRootStateEnable) {
return true;
} else if (systemRootState == kSystemRootStateDisable) {
return false;
}
File f = null;
final String kSuSearchPaths[] = { "/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/" };
try {
for (int i = 0; i < kSuSearchPaths.length; i++) {
f = new File(kSuSearchPaths[i] + "su");
if (f != null && f.exists()) {
systemRootState = kSystemRootStateEnable;
return true;
}
}
} catch (Exception e) {
}
systemRootState = kSystemRootStateDisable;
return false;
}
2. 通过执行测试命令判断是否具有root权限(会产生弹窗)
// 判断机器Android是否已经root,即是否获取root权限
public static boolean haveRoot() {
int i = execRootCmdSilent("echo test"); // 通过执行测试命令来检测
if (i != -1) {
return true;
}
return false;
}
protected static int execRootCmdSilent(String paramString) {
try {
Process localProcess = Runtime.getRuntime().exec("su");
Object localObject = localProcess.getOutputStream();
DataOutputStream localDataOutputStream = new DataOutputStream(
(OutputStream) localObject);
String str = String.valueOf(paramString);
localObject = str + "\n";
localDataOutputStream.writeBytes((String) localObject);
localDataOutputStream.flush();
localDataOutputStream.writeBytes("exit\n");
localDataOutputStream.flush();
localProcess.waitFor();
int result = localProcess.exitValue();
return (Integer) result;
} catch (Exception localException) {
localException.printStackTrace();
return -1;
}
}
3.通过执行su命令来判断Root权限(会产生弹窗)
public 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)
{
e.printStackTrace();
}
}
}