Determine if running on a rooted device

后端 未结 24 2226
無奈伤痛
無奈伤痛 2020-11-22 06:43

My app has a certain piece of functionality that will only work on a device where root is available. Rather than having this feature fail when it is used (and then show an a

24条回答
  •  盖世英雄少女心
    2020-11-22 06:58

    http://code.google.com/p/roottools/

    If you do not want to use the jar file just use the code:

    public static boolean findBinary(String binaryName) {
            boolean found = false;
            if (!found) {
                String[] places = { "/sbin/", "/system/bin/", "/system/xbin/",
                        "/data/local/xbin/", "/data/local/bin/",
                        "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/" };
                for (String where : places) {
                    if (new File(where + binaryName).exists()) {
                        found = true;
    
                        break;
                    }
                }
            }
            return found;
        }
    

    Program will try to find su folder:

    private static boolean isRooted() {
            return findBinary("su");
        }
    

    Example:

    if (isRooted()) {
       textView.setText("Device Rooted");
    
    } else {
       textView.setText("Device Unrooted");
    }
    

提交回复
热议问题