This question already has answers here:
Determine if running on a rooted device
(27个答案)
7年前关闭。
如何检查android设备是否已 Root ?我正在使用以下代码:
当我在设备上运行它时,出现以下异常。
但是在仿真器上运行不会给出任何异常。
我正在使用另一种方法进行检查。在仿真器的命令行中输入
因此,如何检查设备是否已 Root 。
提前致谢。
(27个答案)
7年前关闭。
如何检查android设备是否已 Root ?我正在使用以下代码:
Process proc = Runtime.getRuntime ().exec ("su");
当我在设备上运行它时,出现以下异常。
Causes by:Permission denied
但是在仿真器上运行不会给出任何异常。
我正在使用另一种方法进行检查。在仿真器的命令行中输入
adb shell
会返回#,但是对于设备写入adb shell
则会出现以下错误:shell@android:/ $ su
su
/system/bin/sh: su: not found
127|shell@android:/ $
因此,如何检查设备是否已 Root 。
提前致谢。
最佳答案
我用这个课:
private void CheckRoot()
{
Process p;
try {
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \"Do I have root?\" >/data/LandeRootCheck.txt\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() == 0) {
// TODO Code to run on success
this.IsRoot=true;
}
else {
// TODO Code to run on unsuccessful
this.IsRoot=false;
}
} catch (InterruptedException e) {
// TODO Code to run in interrupted exception
toastMessage("not root");
}
} catch (IOException e) {
// TODO Code to run in input/output exception
toastMessage("not root");
}
}