BaseUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package com.emato.ich.utils;
  2. import android.Manifest;
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageInfo;
  7. import android.content.pm.PackageManager;
  8. import android.content.pm.ResolveInfo;
  9. import android.os.Build;
  10. import android.telephony.SubscriptionInfo;
  11. import android.telephony.SubscriptionManager;
  12. import android.telephony.TelephonyManager;
  13. import android.text.InputType;
  14. import android.text.TextUtils;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.view.inputmethod.InputMethodManager;
  18. import android.widget.EditText;
  19. import androidx.core.app.ActivityCompat;
  20. import androidx.core.content.ContextCompat;
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.lang.reflect.Method;
  27. import java.net.NetworkInterface;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import java.util.concurrent.atomic.AtomicReference;
  31. public class BaseUtils {
  32. private static final String TAG = "BaseUtils";
  33. private static String getMac() {
  34. // TODO 加密传输
  35. // start the ls command running
  36. //String[] args = new String[]{"sh", "-c", command};
  37. Runtime runtime = Runtime.getRuntime();
  38. String command = "cat /sys/class/net/eth0/address";
  39. Process proc = null; //这句话就是shell与高级语言间的调用
  40. try {
  41. proc = runtime.exec(command);
  42. // 如果有参数的话可以用另外一个被重载的exec方法
  43. // 实际上这样执行时启动了一个子进程,它没有父进程的控制台
  44. // 也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
  45. InputStream inputstream = proc.getInputStream();
  46. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  47. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  48. // read the ls output
  49. String line = "";
  50. StringBuilder sb = new StringBuilder(line);
  51. while ((line = bufferedreader.readLine()) != null) {
  52. //System.out.println(line);
  53. sb.append(line);
  54. }
  55. return sb.toString();
  56. } catch (IOException e) {
  57. return "";
  58. }
  59. //tv.setText(sb.toString());
  60. //使用exec执行不会等执行成功以后才返回,它会立即返回
  61. //所以在某些情况下是很要命的(比如复制文件的时候)
  62. //使用waitFor()可以等待命令执行完成以后才返回
  63. // try {
  64. // if (proc.waitFor() != 0) {
  65. // System.err.println("exit value = " + proc.exitValue());
  66. // }
  67. // } catch (InterruptedException e) {
  68. // System.err.println(e);
  69. // }
  70. // return null;
  71. }
  72. public static String getClientId() {
  73. return Md5Utils.string2Md5_16(getMac()).toUpperCase();
  74. // return "285F18D92D0B6568";
  75. }
  76. public static String getMac2() {
  77. try {
  78. List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
  79. for (NetworkInterface nif : all) {
  80. if (!"eth0".equalsIgnoreCase(nif.getName())) {
  81. continue;
  82. }
  83. byte[] macBytes = nif.getHardwareAddress();
  84. if (macBytes == null || macBytes.length == 0) {
  85. continue;
  86. }
  87. StringBuilder result = new StringBuilder();
  88. for (byte b : macBytes) {
  89. result.append(String.format("%02X", b));
  90. }
  91. return result.toString().toUpperCase();
  92. }
  93. } catch (Exception x) {
  94. x.printStackTrace();
  95. }
  96. return "";
  97. }
  98. /**
  99. * 获取当前App的版本号
  100. *
  101. * @param context 上下文
  102. * @return app版本号
  103. */
  104. public static String getAppVersion(Context context) {
  105. PackageManager packageManager = context.getPackageManager();
  106. try {
  107. PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
  108. return packageInfo.versionName;
  109. } catch (Exception e) {
  110. Log.e(TAG, "getAppVersion: 获取APP版本失败!", e);
  111. }
  112. return "";
  113. }
  114. /**
  115. * 获取Android版本号
  116. *
  117. * @return 版本号
  118. */
  119. public static String getVersionName() {
  120. // return "Android " + Build.VERSION.RELEASE;
  121. return Build.VERSION.RELEASE;
  122. }
  123. /**
  124. * 获取IMEI
  125. *
  126. * @param context 程序上下文
  127. * @return imei
  128. */
  129. public static String getIMEI(Context context) {
  130. String imei = "";
  131. //Android 6.0 以后需要获取动态权限 检查权限
  132. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  133. return imei;
  134. }
  135. try {
  136. TelephonyManager manager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  137. if (manager != null) {
  138. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// android 8 即以后建议用getImei 方法获取 不会获取到MEID
  139. Method method = manager.getClass().getMethod("getImei", int.class);
  140. imei = (String) method.invoke(manager, 0);
  141. } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  142. //5.0的系统如果想获取MEID/IMEI1/IMEI2 ----framework层提供了两个属性值“ril.cdma.meid"和“ril.gsm.imei"获取
  143. imei = getSystemPropertyByReflect("ril.gsm.imei");
  144. //如果获取不到 就调用 getDeviceId 方法获取
  145. } else {//5.0以下获取imei/meid只能通过 getDeviceId 方法去取
  146. }
  147. }
  148. } catch (Exception e) {
  149. }
  150. if (TextUtils.isEmpty(imei)) {
  151. String imeiOrMeid = getDeviceId(context);
  152. //长度15 的是imei 14的是meid
  153. if (!TextUtils.isEmpty(imeiOrMeid) && imeiOrMeid.length() >= 15) {
  154. imei = imeiOrMeid;
  155. }
  156. }
  157. return imei;
  158. }
  159. private static String getSystemPropertyByReflect(String key) {
  160. try {
  161. @SuppressLint("PrivateApi")
  162. Class<?> clz = Class.forName("android.os.SystemProperties");
  163. Method getMethod = clz.getMethod("get", String.class, String.class);
  164. return (String) getMethod.invoke(clz, key, "");
  165. } catch (Exception e) {/**/}
  166. return "";
  167. }
  168. public static String getDeviceId(Context context) {
  169. String imei = "";
  170. //Android 6.0 以后需要获取动态权限 检查权限
  171. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  172. return imei;
  173. }
  174. // 1. 尝试通过系统api获取imei
  175. imei = getDeviceIdFromSystemApi(context, 0);
  176. if (TextUtils.isEmpty(imei)) {
  177. imei = getDeviceIdByReflect(context);
  178. }
  179. return imei;
  180. }
  181. @SuppressLint("MissingPermission")
  182. public static String getDeviceIdFromSystemApi(Context context, int slotId) {
  183. String imei = "";
  184. try {
  185. TelephonyManager telephonyManager =
  186. (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  187. if (telephonyManager != null) {
  188. imei = telephonyManager.getDeviceId(slotId);
  189. }
  190. } catch (Throwable e) {
  191. }
  192. return imei;
  193. }
  194. public static String getDeviceIdByReflect(Context context) {
  195. try {
  196. TelephonyManager tm = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  197. if (Build.VERSION.SDK_INT >= 21) {
  198. Method simMethod = TelephonyManager.class.getDeclaredMethod("getDefaultSim");
  199. Object sim = simMethod.invoke(tm);
  200. Method method = TelephonyManager.class.getDeclaredMethod("getDeviceId", int.class);
  201. return method.invoke(tm, sim).toString();
  202. } else {
  203. Class<?> clazz = Class.forName("com.android.internal.telephony.IPhoneSubInfo");
  204. Method subInfoMethod = TelephonyManager.class.getDeclaredMethod("getSubscriberInfo");
  205. subInfoMethod.setAccessible(true);
  206. Object subInfo = subInfoMethod.invoke(tm);
  207. Method method = clazz.getDeclaredMethod("getDeviceId");
  208. return method.invoke(subInfo).toString();
  209. }
  210. } catch (Throwable e) {
  211. }
  212. return "";
  213. }
  214. public static String getIp(){
  215. Runtime runtime = Runtime.getRuntime();
  216. String command = "netcfg";
  217. Process proc = null; //这句话就是shell与高级语言间的调用
  218. try {
  219. proc = runtime.exec(command);
  220. // 如果有参数的话可以用另外一个被重载的exec方法
  221. // 实际上这样执行时启动了一个子进程,它没有父进程的控制台
  222. // 也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
  223. InputStream inputstream = proc.getInputStream();
  224. InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  225. BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  226. // read the ls output
  227. String line = "";
  228. StringBuilder sb = new StringBuilder(line);
  229. while ((line = bufferedreader.readLine()) != null) {
  230. //System.out.println(line);
  231. sb.append(line);
  232. }
  233. return sb.toString();
  234. } catch (IOException e) {
  235. return "";
  236. }
  237. }
  238. public static void disableEditText(EditText editText) {
  239. if (Build.VERSION.SDK_INT <= 10) {
  240. editText.setInputType(InputType.TYPE_NULL);
  241. } else {
  242. Class<EditText> editTextClass = EditText.class;
  243. Method method;
  244. try {
  245. method = editTextClass.getMethod("setShowSoftInputOnFocus", boolean.class);
  246. method.setAccessible(true);
  247. method.invoke(editText, false);
  248. } catch (Exception e) {
  249. }
  250. try {
  251. method = editTextClass.getMethod("setSoftInputShownOnFocus", boolean.class);
  252. method.setAccessible(true);
  253. method.invoke(editText, false);
  254. } catch (Exception e) {
  255. }
  256. }
  257. }
  258. }