Determine if running on a rooted device

后端 未结 24 2543
無奈伤痛
無奈伤痛 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:06

    Indeed it is interesting question and so far nobody has deserved award. I use the following code:

      boolean isRooted() {
          try {
                    ServerSocket ss = new ServerSocket(81);
                    ss.close();
                                        return true;
                } catch (Exception e) {
                    // not sure
                }
        return false;
      }
    

    The code is certainly not bulletproof, because network can be not available so you get an exception. If this method returns true then 99% you can be sure, otherwise just 50% that not. Networking permission can also spoil the solution.

提交回复
热议问题