0
0

BaseUtils.java 9.8 KB

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