当前位置:   article > 正文

获取Android 手机设备信息:包括机型、操作系统版本号、手机分辨率、运营商、当前联网方式、IMEI、MEID、MAC地址_c# 获取imei即获取移动设备手机串号手机编码号源代码

c# 获取imei即获取移动设备手机串号手机编码号源代码

获取Android 手机设备信息:操作系统版本号、手机分辨率、运营商、当前联网方式、IMEI、MEID、MAC地址

包括双卡机型上的两个IMEI信息

  1. /**
  2. * Created by Administrator on 2019/5/15.
  3. * 手机详细信息
  4. */
  5. public class DeviceInfoModel {
  6. private static final String TAG = "DeviceInfoModel";
  7. private static DeviceInfoModel instance;
  8. public DeviceInfoModel() {
  9. }
  10. public static DeviceInfoModel getInstance() {
  11. if (instance == null) {
  12. instance = new DeviceInfoModel();
  13. }
  14. return instance;
  15. }
  16. /**
  17. * 获取机型
  18. */
  19. public String getPhoneModel() {
  20. // TelephonyManager manager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  21. // String mtype = android.os.Build.MODEL;
  22. String brand = android.os.Build.BRAND;//手机品牌
  23. String model = android.os.Build.MODEL;//手机型号
  24. Log.w(TAG, "手机型号:" + brand + " " + model);
  25. return brand + " " + model;
  26. }
  27. /**
  28. * 获取操作系统
  29. *
  30. * @return
  31. */
  32. public String getOS() {
  33. Log.w(TAG, "操作系统:" + "Android" + android.os.Build.VERSION.RELEASE);
  34. return "Android" + android.os.Build.VERSION.RELEASE;
  35. }
  36. /**
  37. * 获取手机分辨率
  38. *
  39. * @param context
  40. * @return
  41. */
  42. public String getResolution(Context context) {
  43. // 方法1 Android获得屏幕的宽和高
  44. WindowManager windowManager = ((Activity) context).getWindowManager();
  45. Display display = windowManager.getDefaultDisplay();
  46. int screenWidth = display.getWidth();
  47. int screenHeight = display.getHeight();
  48. Log.w(TAG, "分辨率:" + screenWidth + "*" + screenHeight);
  49. return screenWidth + "*" + screenHeight;
  50. }
  51. /**
  52. * 获取唯一设备号
  53. */
  54. public String getDeviceNo(Context context) {
  55. if (!checkReadPhoneStatePermission(context)) {
  56. Log.w(TAG, "获取唯一设备号: 无权限");
  57. return "";
  58. }
  59. TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
  60. // @SuppressLint("HardwareIds") String szImei = TelephonyMgr.getDeviceId();
  61. Method method = null;
  62. String imei2 = "";
  63. try {
  64. method = TelephonyMgr.getClass().getMethod("getDeviceId", int.class);
  65. String imei1 = TelephonyMgr.getDeviceId();
  66. String imei0 = (String) method.invoke(TelephonyMgr, 0);
  67. imei2 = (String) method.invoke(TelephonyMgr, 1);
  68. String meid = (String) method.invoke(TelephonyMgr, 2);
  69. Log.e(TAG, "唯一设备号szImei-0 is :" + imei0 + " --- imei1: " + imei1 + " --- imei2: " + imei2 + " -meid is :" + meid);
  70. } catch (NoSuchMethodException e) {
  71. e.printStackTrace();
  72. Log.w(TAG, "唯一设备号imei-NoSuchMethodException: " + e.getMessage());
  73. } catch (IllegalAccessException e) {
  74. e.printStackTrace();
  75. Log.w(TAG, "唯一设备号imei-IllegalAccessException: " + e.getMessage());
  76. } catch (InvocationTargetException e) {
  77. e.printStackTrace();
  78. Log.w(TAG, "唯一设备号imei-InvocationTargetException: " + e.getMessage());
  79. }
  80. return imei2;
  81. }
  82. /**
  83. * 获取运营商
  84. *
  85. * @param context
  86. * @return
  87. */
  88. public String getNetOperator(Context context) {
  89. TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  90. String iNumeric = manager.getSimOperator();
  91. String netOperator = "";
  92. if (iNumeric.length() > 0) {
  93. if (iNumeric.equals("46000") || iNumeric.equals("46002")) {
  94. // 中国移动
  95. netOperator = "中国移动";
  96. } else if (iNumeric.equals("46003")) {
  97. // 中国电信
  98. netOperator = "中国电信";
  99. } else if (iNumeric.equals("46001")) {
  100. // 中国联通
  101. netOperator = "中国联通";
  102. } else {
  103. //未知
  104. netOperator = "未知";
  105. }
  106. }
  107. Log.w(TAG, "运营商:" + netOperator);
  108. return netOperator;
  109. }
  110. /**
  111. * 获取联网方式
  112. */
  113. public String getNetMode(Context context) {
  114. String strNetworkType = "未知";
  115. // TelephonyManager manager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  116. // manager.getNetworkType();
  117. ConnectivityManager manager = (ConnectivityManager) context.
  118. getSystemService(Context.CONNECTIVITY_SERVICE);
  119. NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  120. if (networkInfo != null && networkInfo.isConnected()) {
  121. int netMode = networkInfo.getType();
  122. if (netMode == ConnectivityManager.TYPE_WIFI) {
  123. strNetworkType = "WIFI";
  124. //wifi
  125. } else if (netMode == ConnectivityManager.TYPE_MOBILE) {
  126. int networkType = networkInfo.getSubtype();
  127. switch (networkType) {
  128. //2g
  129. case TelephonyManager.NETWORK_TYPE_GPRS:
  130. case TelephonyManager.NETWORK_TYPE_EDGE:
  131. case TelephonyManager.NETWORK_TYPE_CDMA:
  132. case TelephonyManager.NETWORK_TYPE_1xRTT:
  133. case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
  134. strNetworkType = "2G";
  135. break;
  136. //3g
  137. case TelephonyManager.NETWORK_TYPE_UMTS:
  138. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  139. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  140. case TelephonyManager.NETWORK_TYPE_HSDPA:
  141. case TelephonyManager.NETWORK_TYPE_HSUPA:
  142. case TelephonyManager.NETWORK_TYPE_HSPA:
  143. case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
  144. case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 : replace by 12
  145. case TelephonyManager.NETWORK_TYPE_HSPAP: //api<13 : replace by 15
  146. strNetworkType = "3G";
  147. break;
  148. case TelephonyManager.NETWORK_TYPE_LTE: //api<11 : replace by 13
  149. strNetworkType = "4G";
  150. break;
  151. default:
  152. String _strSubTypeName = networkInfo.getSubtypeName();
  153. // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
  154. if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
  155. strNetworkType = "3G";
  156. } else {
  157. strNetworkType = _strSubTypeName;
  158. }
  159. break;
  160. }
  161. }
  162. }
  163. Log.w(TAG, "联网方式:" + strNetworkType);
  164. return strNetworkType;
  165. }
  166. private boolean checkReadPhoneStatePermission(Context context) {
  167. try {
  168. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
  169. != PackageManager.PERMISSION_GRANTED) {
  170. ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE},
  171. 10);
  172. return false;
  173. }
  174. } catch (IllegalArgumentException e) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. @SuppressLint({"MissingPermission", "HardwareIds"})
  180. public String getMEID(Context context) {
  181. if (!checkReadPhoneStatePermission(context)) {
  182. Log.w(TAG, "获取唯一设备号-getMEID: 无权限");
  183. return "";
  184. }
  185. String meid = "";
  186. TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  187. if (null != mTelephonyMgr) {
  188. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  189. meid = mTelephonyMgr.getMeid();
  190. Log.i(TAG, "Android版本大于o-26-优化后的获取---meid:" + meid);
  191. } else {
  192. meid = mTelephonyMgr.getDeviceId();
  193. }
  194. }
  195. Log.i(TAG, "优化后的获取---meid:" + meid);
  196. return meid;
  197. }
  198. @SuppressLint("MissingPermission")
  199. public String getIMEI(Context context) {
  200. if (!checkReadPhoneStatePermission(context)) {
  201. Log.w(TAG, "获取唯一设备号-getIMEI: 无权限");
  202. return "";
  203. }
  204. String imei1 = "";
  205. TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  206. if (null != mTelephonyMgr) {
  207. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  208. imei1 = mTelephonyMgr.getImei(0);
  209. Log.i(TAG, "Android版本大于o-26-优化后的获取---imei-1:" + imei1);
  210. } else {
  211. try {
  212. imei1 = getDoubleImei(mTelephonyMgr, "getDeviceIdGemini", 0);
  213. } catch (Exception e) {
  214. try {
  215. imei1 = getDoubleImei(mTelephonyMgr, "getDeviceId", 0);
  216. } catch (Exception e1) {
  217. e1.printStackTrace();
  218. }
  219. Log.e(TAG, "get device id fail: " + e.toString());
  220. }
  221. }
  222. }
  223. Log.i(TAG, "优化后的获取---imei1:" + imei1);
  224. return imei1;
  225. }
  226. @SuppressLint("MissingPermission")
  227. public String getIMEI2(Context context) {
  228. if (!checkReadPhoneStatePermission(context)) {
  229. Log.w(TAG, "获取唯一设备号-getIMEI2: 无权限");
  230. return "";
  231. }
  232. String imei2 = "";
  233. TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  234. if (null != mTelephonyMgr) {
  235. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  236. imei2 = mTelephonyMgr.getImei(1);
  237. mTelephonyMgr.getMeid();
  238. Log.i(TAG, "Android版本大于o-26-优化后的获取---imei-2:" + imei2);
  239. } else {
  240. try {
  241. imei2 = getDoubleImei(mTelephonyMgr, "getDeviceIdGemini", 1);
  242. } catch (Exception e) {
  243. try {
  244. imei2 = getDoubleImei(mTelephonyMgr, "getDeviceId", 1);
  245. } catch (Exception ex) {
  246. Log.e(TAG, "get device id fail: " + e.toString());
  247. }
  248. }
  249. }
  250. }
  251. Log.i(TAG, "优化后的获取--- imei2:" + imei2);
  252. return imei2;
  253. }
  254. /**
  255. * 获取双卡手机的imei
  256. */
  257. private String getDoubleImei(TelephonyManager telephony, String predictedMethodName, int slotID) throws Exception {
  258. String inumeric = null;
  259. Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
  260. Class<?>[] parameter = new Class[1];
  261. parameter[0] = int.class;
  262. Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
  263. Object[] obParameter = new Object[1];
  264. obParameter[0] = slotID;
  265. Object ob_phone = getSimID.invoke(telephony, obParameter);
  266. if (ob_phone != null) {
  267. inumeric = ob_phone.toString();
  268. }
  269. return inumeric;
  270. }
  271. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/221099
推荐阅读
相关标签
  

闽ICP备14008679号