是否有可能以编程方式确定Android设备是否已植根?
我想检查一个android设备是否是根源的。 如果设备是根植的,我不希望我的应用程序向用户显示适当的消息,并且应用程序不应该在根设备上工作。
我已经通过各种链接和博客,其中有代码snipplets来检查设备是否是根源。 但我也发现多位开发人员说,无法以编程方式检查设备是否已植根或无法确定。 代码片段可能无法在所有设备上提供100%准确的结果,结果也可能取决于用于生成android设备的工具。
请让我知道是否有任何方法确认设备是否已植根或不是以编程方式。
谢谢,萨加尔
我没有足够的声望点评论,所以我必须添加另一个答案。
CodeMonkey的帖子中的代码适用于大多数设备,但至少在Nexus 5上使用Marshmallow并不适用,因为即使在非root用户的设备上,哪个命令也能正常工作。 但是因为su不起作用,它会返回一个非零的退出值。 这段代码虽然期望有一个例外,所以它必须像这样修改:
private static boolean canExecuteCommand(String command) {
    try {
        int exitValue = Runtime.getRuntime().exec(command).waitFor();
        return exitValue == 0;
    } catch (Exception e) {
        return false;
    }
}
可能重复的Stackoverflow。
这一个有一个答案
在第二个链接上回答。 这家伙在大约10台设备上进行了测试,并为他工作。
  /**
   * Checks if the device is rooted.
   *
   * @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
   */
  public static boolean isRooted() {
    // get from build info
    String buildTags = android.os.Build.TAGS;
    if (buildTags != null && buildTags.contains("test-keys")) {
      return true;
    }
    // check if /system/app/Superuser.apk is present
    try {
      File file = new File("/system/app/Superuser.apk");
      if (file.exists()) {
        return true;
      }
    } catch (Exception e1) {
      // ignore
    }
    // try executing commands
    return canExecuteCommand("/system/xbin/which su")
        || canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su");
  }
  // executes a command on the system
  private static boolean canExecuteCommand(String command) {
    boolean executedSuccesfully;
    try {
      Runtime.getRuntime().exec(command);
      executedSuccesfully = true;
    } catch (Exception e) {
      executedSuccesfully = false;
    }
    return executedSuccesfully;
  }
上一篇: Is it possible to programmatically say for sure if an android device is rooted?
下一篇: Prevent application from Root Cloak to hide root access
