Determine if running on a rooted device

后端 未结 24 2229
無奈伤痛
無奈伤痛 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 07:12

    Using my library at rootbox, it is pretty easy. Check the required code below:

        //Pass true to .start(...) call to run as superuser
        Shell shell = null;
        try {
                shell = Shell.start(true);
        } catch (IOException exception) {
                exception.printStackTrace();
        }
        if (shell == null)
                // We failed to execute su binary
                return;
        if (shell.isRoot()) {
                // Verified running as uid 0 (root), can continue with commands
                ...
        } else
                throw Exception("Unable to gain root access. Make sure you pressed Allow/Grant in superuser prompt.");
    

提交回复
热议问题