我创建了一个方法来检查Android手机是否已root。这是按如下方式完成的
public int checkrootcommand(String string) {
// TODO Auto-generated method stub
Process exec;
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c"});
final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
out.write("exit");
out.flush();
Log.i(SUPER_USER_COMMAND, "su command executed successfully");
return 0; // returns zero when the command is executed successfully
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1; //returns one when the command execution fails
}
但问题是,首先执行方法 checkrootcommand() 它工作正常,但是当再次调用相同的方法时,超级用户会话仍在运行。有没有办法在方法执行后结束超级用户会话??
没有可靠的方法来检测通过利用软件漏洞克服硬件保护的设备上的 root 情况。
您最多可以检测特定工具集的存在或扫描不应该存在的东西或文件中的更改 - 但这需要了解给定的安装应该是什么样子,并假设您的操作系统功能用于使检查未被修改以隐藏更改。为了可靠地扫描,您需要确保可信代码的运行级别低于不可信代码;获得 root 权限的设备是这种保证从根本上被破坏的设备,或者最终用户比开发人员更受信任的设备。
public int checkrootcommand(String string) {
// TODO Auto-generated method stub
Process exec = null;
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c"});
final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
out.write("exit");
out.flush();
Log.i(SUPER_USER_COMMAND, "su command executed successfully");
return 0; // returns zero when the command is executed successfully
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (exec != null) {
try {
exec.destroy();
} catch (Exception ignored) {
}
}
}
return 1; //returns one when the command execution fails
}
您可能无法普遍检测手机是否已root,但您应该能够请求并确认您的应用程序是否可以通过以root身份运行id来访问root,例如,
su -c id
验证命令是否成功执行以及输出包含
uid=0
即root 用户的 uid。
方法1:应用程序要求ROOT访问权限:
将其添加到您的应用程序级 gradle 构建文件中:
dependencies {
compile 'eu.chainfire:libsuperuser:201501111220'
}
现在,
System.out.println(Shell.Su.available());
//Outputs true if user-granted else false
方法2:应用程序不要求ROOT:
boolean deviceisRooted() {
String[] filespaths = {"/system/app/Superuser.apk","/sbin/su", "/system/bin/su","/system/xbin/su"};
for (String xyz : filespaths) {
if (new File(xyz).exists()) return true;
}
return false;
}
System.out.prinln(deviceisRooted());
//Outputs true if device is ROOTED else false
//Doesn't asks user
//Also returns true IF NOT PROPERLY ROOTED (But ROOTED somehow)
Process executor = Runtime.getRuntime().exec("su -c ls /data/data");
executor.waitFor();
int iabd = executor.exitValue();
if(iabd != 0){ /*process exit value is not 0, so user is not root*/ }else{ /* user is root*/ }
if [ ! -z "$(/system/bin/ps -A | grep -v grep | grep -c daemonsu)" ]; then echo "device is rooted"; else echo "device is not rooted"; fi
您的应用程序也不需要这种方式的 root 访问权限。
Lexip的解决方案与扫描仪工作正常。
完成他的回复:
boolean isRooted = false;
boolean isRootGranted = false;
try {
Process process = Runtime.getRuntime().exec("su -c ls"); // Ask for su and list
Scanner scanner = new Scanner(process.getInputStream()).useDelimiter("\\A");
if (scanner.hasNext()) isRootGranted = true;
process.waitFor();
process.destroy();
isRooted = true;
if (isRootGranted) Toast.makeText(MainActivity.this, "ROOT granted", Toast.LENGTH_SHORT).show();
else Toast.makeText(MainActivity.this, "ROOT not granted", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
isRooted = false;
Toast.makeText(MainActivity.this, "ERROR #SU AUTORISATION\nPHONE NON ROOTED", Toast.LENGTH_SHORT).show();
}
isRooted = true(在已 root 的手机上),如果没有,则为 false。
如果授予超级用户访问权限,则 isRootGranted = true;如果没有,则为 false。
每次你打电话时都在工作。
问候。