获取 IMEI 码 @return IMEI 码
()
| 2062 | * @return IMEI 码 |
| 2063 | */ |
| 2064 | public static String getIMEI() { |
| 2065 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 2066 | ShellUtils.CommandResult result = ShellUtils.execCmd( |
| 2067 | "service call iphonesubinfo 1", true |
| 2068 | ); |
| 2069 | if (result.isSuccessWithSuccessOutput()) { |
| 2070 | try { |
| 2071 | StringBuilder builder = new StringBuilder(); |
| 2072 | String subStr = result.successMsg.replaceAll("\\.", ""); |
| 2073 | subStr = subStr.substring(subStr.indexOf('\'') + 1, subStr.indexOf("')")); |
| 2074 | // 添加数据 |
| 2075 | builder.append(subStr.substring(0, subStr.indexOf('\''))); |
| 2076 | // 从指定索引开始 |
| 2077 | int index = subStr.indexOf("'", builder.length() + 1); |
| 2078 | // 再次裁剪 |
| 2079 | subStr = subStr.substring(index + 1); |
| 2080 | // 添加数据 |
| 2081 | builder.append(subStr.substring(0, subStr.indexOf("'"))); |
| 2082 | // 从指定索引开始 |
| 2083 | index = subStr.indexOf("'", builder.length() + 1); |
| 2084 | // 再次裁剪 |
| 2085 | subStr = subStr.substring(index + 1); |
| 2086 | // 最后进行添加 |
| 2087 | builder.append(subStr.split(DevFinal.REGEX.SPACE)[0]); |
| 2088 | // 返回对应的数据 |
| 2089 | return builder.toString(); |
| 2090 | } catch (Exception e) { |
| 2091 | LogPrintUtils.eTag(TAG, e, "getIMEI"); |
| 2092 | } |
| 2093 | } |
| 2094 | } else { |
| 2095 | // 在 Android 4.4 及以下版本可通过如下命令获取 IMEI |
| 2096 | ShellUtils.CommandResult result = ShellUtils.execCmd( |
| 2097 | "dumpsys iphonesubinfo", true |
| 2098 | ); |
| 2099 | if (result.isSuccessWithSuccessOutput()) { // 返回值中的 Device ID 就是 IMEI |
| 2100 | try { |
| 2101 | String[] arrays = result.successMsg.split(DevFinal.SYMBOL.NEW_LINE); |
| 2102 | for (String value : arrays) { |
| 2103 | if (!TextUtils.isEmpty(value)) { |
| 2104 | if (value.toLowerCase().contains("device")) { |
| 2105 | // 进行拆分 |
| 2106 | String[] splitArray = value.split(DevFinal.REGEX.SPACE); |
| 2107 | return splitArray[splitArray.length - 1]; |
| 2108 | } |
| 2109 | } |
| 2110 | } |
| 2111 | } catch (Exception e) { |
| 2112 | LogPrintUtils.eTag(TAG, e, "getIMEI"); |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | return null; |
| 2117 | } |
| 2118 | |
| 2119 | /** |
| 2120 | * 获取 IP 地址 |
nothing calls this directly
no test coverage detected