[Android] does not play the root request box to check whether the mobile phone is root or androidroot.
Because the project requires root installation software, and you want to guide the user to enable root installation when appropriate, you need to check whether the mobile phone is root.
The basic judgment is as follows: directly run an underlying command. (Reference https://github.com/Trinea/android-common/blob/master/src/cn/trinea/android/common/util/ShellUtils.java)
For more information, see csdnhttp: // blog.csdn.net/fm9333/article/details/12752415.
1 /** 2 * check whether has root permission 3 * 4 * @return 5 */ 6 public static boolean checkRootPermission() { 7 return execCommand("echo root", true, false).result == 0; 8 } 9 10 11 /** 12 * execute shell commands 13 * 14 * @param commands 15 * command array 16 * @param isRoot 17 * whether need to run with root 18 * @param isNeedResultMsg 19 * whether need result msg 20 * @return <ul> 21 * <li>if isNeedResultMsg is false, {@link CommandResult#successMsg} 22 * is null and {@link CommandResult#errorMsg} is null.</li> 23 * <li>if {@link CommandResult#result} is -1, there maybe some 24 * excepiton.</li> 25 * </ul> 26 */ 27 public static CommandResult execCommand(String[] commands, boolean isRoot, 28 boolean isNeedResultMsg) { 29 int result = -1; 30 if (commands == null || commands.length == 0) { 31 return new CommandResult(result, null, null); 32 } 33 34 Process process = null; 35 BufferedReader successResult = null; 36 BufferedReader errorResult = null; 37 StringBuilder successMsg = null; 38 StringBuilder errorMsg = null; 39 40 DataOutputStream os = null; 41 try { 42 process = Runtime.getRuntime().exec( 43 isRoot ? COMMAND_SU : COMMAND_SH); 44 os = new DataOutputStream(process.getOutputStream()); 45 for (String command : commands) { 46 if (command == null) { 47 continue; 48 } 49 50 // donnot use os.writeBytes(commmand), avoid chinese charset 51 // error 52 os.write(command.getBytes()); 53 os.writeBytes(COMMAND_LINE_END); 54 os.flush(); 55 } 56 os.writeBytes(COMMAND_EXIT); 57 os.flush(); 58 59 result = process.waitFor(); 60 // get command result 61 if (isNeedResultMsg) { 62 successMsg = new StringBuilder(); 63 errorMsg = new StringBuilder(); 64 successResult = new BufferedReader(new InputStreamReader( 65 process.getInputStream())); 66 errorResult = new BufferedReader(new InputStreamReader( 67 process.getErrorStream())); 68 String s; 69 while ((s = successResult.readLine()) != null) { 70 successMsg.append(s); 71 } 72 while ((s = errorResult.readLine()) != null) { 73 errorMsg.append(s); 74 } 75 } 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } finally { 81 try { 82 if (os != null) { 83 os.close(); 84 } 85 if (successResult != null) { 86 successResult.close(); 87 } 88 if (errorResult != null) { 89 errorResult.close(); 90 } 91 } catch (IOException e) { 92 e.printStackTrace(); 93 } 94 95 if (process != null) { 96 process.destroy(); 97 } 98 } 99 return new CommandResult(result, successMsg == null ? null100 : successMsg.toString(), errorMsg == null ? null101 : errorMsg.toString()); 102 } 103 104 /**105 * result of command, 106 * <ul> 107 * <li>{@link CommandResult#result} means result of command, 0 means normal, 108 * else means error, same to excute in linux shell</li> 109 * <li>{@link CommandResult#successMsg} means success message of command 110 * result</li> 111 * <li>{@link CommandResult#errorMsg} means error message of command result</li> 112 * </ul> 113 * 114 * @author Trinea 2013-5-16 115 */116 public static class CommandResult { 117 118 /** result of command **/119 public int result; 120 /** success message of command result **/121 public String successMsg; 122 /** error message of command result **/123 public String errorMsg; 124 125 public CommandResult(int result) { 126 this.result = result; 127 } 128 129 public CommandResult(int result, String successMsg, String errorMsg) { 130 this.result = result; 131 this.successMsg = successMsg; 132 this.errorMsg = errorMsg; 133 } 134 } /**135 * execute shell command, default return result msg 136 * 137 * @param command 138 * command 139 * @param isRoot 140 * whether need to run with root 141 * @return142 * @see ShellUtils#execCommand(String[], boolean, boolean) 143 */144 public static CommandResult execCommand(String command, boolean isRoot) { 145 return execCommand(new String[] { command }, isRoot, true); 146 }
However, this may cause a problem. A root request box will pop up every time you determine whether the root is used. This is a very unfriendly interaction method. If you choose to cancel, some mobile phones are regarded as non-root.
This is method 1. Unfriendly interaction and misjudgment.
In this case, in order not to bring up a confirmation box, the root phone usually has some special folders, such as/system/bin/su,/system/xbin/su, it stores related permission control files.
Therefore, as long as there is a folder in the mobile phone, the root of the mobile phone is determined.
After testing, this method is feasible on most mobile phones.
The Code is as follows:
1/** determine whether the ROOT permission is granted. This method is invalid for some mobile phones, such as Xiaomi series */2 public static boolean isRoot () {3 4 boolean res = false; 5 6 try {7 if ((! New File ("/system/bin/su"). exists () 8 &&(! New File ("/system/xbin/su "). exists () {9 res = false; 10} else {11 res = true; 12} 13; 14} catch (Exception e) {15 res = false; 16} 17 return res; 18}
This is method 2. Interactive friendliness, but misjudgment.
Later, during the test, some Chinese products, such as the Xiaomi series, were found to have this folder, but the system was not root and it was determined to have become root. After analysis, this is because Xiaomi has its own permission control system.
Considering that Xiaomi mobile phones have a large number of user groups, this problem must be solved, so we have to find a third solution.
Starting from the principle, whether the phone is root or not, it should have related files. The reason is that the file has the relevant permissions. The user group cannot execute related files.
From this perspective, you can start with determining the file permissions.
Let's take a look at the file permissions in linux.
Linux File permissions for details can refer to the "Uncle bird linux private dish" http://vbird.dic.ksu.edu.tw/linux_basic/0210filepermission.php#filepermission_perm
On the basis of the second method, you only need to determine whether the file owner has the executable permission (4th characters in status) for the file, and then determine whether the mobile phone is root.
On a root phone (Samsung i9100 android 4.4), file permissions (x or s, s permissions, refer to the http://blog.chinaunix.net/uid-20809581-id-3141879.html) are as follows
Not the root phone. Most mobile phones do not have these two folders. Xiaomi mobile phones have these folders. The permissions for the root Xiaomi phone are as follows (because there is no Xiaomi phone at hand, you can make up the phone in a few days, or you can help me make up the phone ).
[Waiting for picture supplement]
The Code is as follows:
1/** determine whether the mobile phone is root. The root request box is not displayed. <br/> */2 public static boolean isRoot () {3 String binPath = "/system/bin/su"; 4 String xBinPath = "/system/xbin/su"; 5 if (new File (binPath ). exists () & isExecutable (binPath) 6 return true; 7 if (new File (xBinPath ). exists () & isExecutable (xBinPath) 8 return true; 9 return false; 10} 11 12 private static boolean isExecutable (String filePath) {13 Process p = null; 1 4 try {15 p = runtime.getruntime(cmd.exe c ("ls-l" + filePath); 16 // get the returned content 17 BufferedReader in = new BufferedReader (new InputStreamReader (18 p. getInputStream (); 19 String str = in. readLine (); 20 Log. I (TAG, str); 21 if (str! = Null & str. length ()> = 4) {22 char flag = str. charAt (3); 23 if (flag = 's' | flag = 'X') 24 return true; 25} 26} catch (IOException e) {27 e. printStackTrace (); 28} finally {29 if (p! = Null) {30 p. destroy (); 31} 32} 33 return false; 34}
This method can basically determine all mobile phones, and does not pop up the root request box. This is what we need, perfect.
Method 3: Interactive and user-friendly.
The following is the apk and related source code. You can download the apk to view the running effect.
ROOT detection APK: http://good.gd/3091610.htm
Download the ROOT check code: http://good.gd/3091609.htm?http://download.csdn.net/detail/waylife/7639017
If you cannot determine how to use mobile phone 3, please submit it.
You are also welcome to propose other better methods.
Q: customized IT education platform, one-to-one service, Q & A. Official Website for developing programming social headlines: www.wenaaa.com
QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!