firemail

标题: 判断手机是否有root权限 [打印本页]

作者: java    时间: 2018-7-13 21:06
标题: 判断手机是否有root权限
  1. public class RootUtil {
  2.     public static boolean isDeviceRooted() {
  3.         return checkRootMethod1() || checkRootMethod2() || checkRootMethod3();
  4.     }

  5.     private static boolean checkRootMethod1() {
  6.         String buildTags = android.os.Build.TAGS;
  7.         return buildTags != null && buildTags.contains("test-keys");
  8.     }

  9.     private static boolean checkRootMethod2() {
  10.         String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su",
  11.                 "/system/bin/failsafe/su", "/data/local/su", "/su/bin/su"};
  12.         for (String path : paths) {
  13.             if (new File(path).exists()) return true;
  14.         }
  15.         return false;
  16.     }

  17.     private static boolean checkRootMethod3() {
  18.         Process process = null;
  19.         try {
  20.             process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
  21.             BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  22.             if (in.readLine() != null) return true;
  23.             return false;
  24.         } catch (Throwable t) {
  25.             return false;
  26.         } finally {
  27.             if (process != null) process.destroy();
  28.         }
  29.     }

  30. }
复制代码
调用
  1. if (isDeviceRooted()){
  2.             Log.d(TAG, "onCreate: 你的设备可以获取root");
  3.         }else {
  4.             Log.d(TAG, "onCreate: 你的设备可以获取不能获取root");
  5. }
复制代码





欢迎光临 firemail (http://firemail.wang:8088/) Powered by Discuz! X3