当前位置:   article > 正文

Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)_安卓读取已经安装设备信息

安卓读取已经安装设备信息

目录

获取设备id

获取ip地址

获取设备名称

获取设备型号

获取设备处理器

获取设备品牌

获取设备制造商

获取设备oaid

获取设备识别码

获取mac地址

获取应用包名

获取应用签名

获取app版本

获取版本号

获取分辨率

获取国家码

获取系统语言

获取设备网络代码

获取设备网络类型

获取android版本

获取操作系统版本

获取目标sdk版本号

获取应用第一次安装时间


获取设备id

  1. public static String getDeviceId() {
  2. if (context == null) {
  3. return "";
  4. }
  5. return DeviceIdentifier.getAndroidID(context);
  6. }

获取ip地址

  1. public static String getIp() {
  2. String networkType = getNetworkType();
  3. if (networkType.equals("WiFi")) {
  4. WifiManager wifiManager = null;
  5. try {
  6. wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  7. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  8. int i = wifiInfo.getIpAddress();
  9. return intToIp(i);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. return null;
  13. }
  14. } else if (networkType.equals("2G") || networkType.equals("3G") || networkType.equals("4G")) {
  15. try {
  16. for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
  17. NetworkInterface intf = en.nextElement();
  18. for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
  19. InetAddress inetAddress = enumIpAddr.nextElement();
  20. if (!inetAddress.isLoopbackAddress()) {
  21. return inetAddress.getHostAddress().toString();
  22. }
  23. }
  24. }
  25. } catch (Exception ex) {
  26. return null;
  27. }
  28. } else {
  29. return null;
  30. }
  31. return null;
  32. }
  33. /**
  34. * 将ip的整数形式转换成ip形式
  35. */
  36. public static String intToIp(int i) {
  37. return (i & 0xFF) + "." +
  38. ((i >> 8) & 0xFF) + "." +
  39. ((i >> 16) & 0xFF) + "." +
  40. (i >> 24 & 0xFF);
  41. }

获取设备名称

  1. public static String getDeviceName() {
  2. return Build.DEVICE;
  3. }

获取设备型号

  1. public static String getDeviceModel() {
  2. return Build.MODEL;
  3. }

获取设备处理器

  1. public static String getDeviceBoard() {
  2. return Build.BOARD;
  3. }

获取设备品牌

  1. public static String getDeviceBrand() {
  2. return Build.BRAND;
  3. }

获取设备制造商

  1. public static String getDeviceManufacturer() {
  2. return Build.MANUFACTURER;
  3. }

获取设备oaid

  1. public static String getOAID() {
  2. if (context == null) {
  3. return "";
  4. }
  5. if (DeviceID.supportedOAID(context)) {
  6. return DeviceID.getOAID();
  7. }
  8. return "";
  9. }

获取设备识别码

  1. public static String getImei() {
  2. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  3. if (tm == null) {
  4. return "";
  5. }
  6. return tm.getDeviceId();
  7. }

获取mac地址

  1. public static String getMac() {
  2. if (context == null) {
  3. return "";
  4. }
  5. WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  6. WifiInfo wi = wm.getConnectionInfo();
  7. if (wi == null || wi.getMacAddress() == null) {
  8. return "";
  9. }
  10. return wi.getMacAddress().trim();
  11. }

获取应用包名

  1. public static String getPackageName() {
  2. if (context == null) {
  3. return "";
  4. }
  5. return context.getPackageName();
  6. }

获取应用签名

  1. public static String getAppSignature() {
  2. if (context == null) {
  3. return "";
  4. }
  5. byte[] cert = new byte[0];
  6. try {
  7. PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
  8. cert = packageInfo.signatures[0].toByteArray();
  9. } catch (PackageManager.NameNotFoundException e) {
  10. e.printStackTrace();
  11. }
  12. return cert.toString();
  13. }

获取app版本

  1. public static String getAppVersion() {
  2. if (context == null) {
  3. return "";
  4. }
  5. PackageManager pm = context.getPackageManager();
  6. PackageInfo pi = null;
  7. try {
  8. pi = pm.getPackageInfo(context.getPackageName(), 0);
  9. } catch (PackageManager.NameNotFoundException e) {
  10. e.printStackTrace();
  11. }
  12. return pi.versionName;
  13. }

获取版本号

  1. public static int getVersionCode() {
  2. if (context == null) {
  3. return 0;
  4. }
  5. PackageManager pm = context.getPackageManager();
  6. PackageInfo pi = null;
  7. try {
  8. pi = pm.getPackageInfo(context.getPackageName(), 0);
  9. } catch (PackageManager.NameNotFoundException e) {
  10. e.printStackTrace();
  11. }
  12. return pi.versionCode;
  13. }

获取分辨率

  1. public static String getResolution() {
  2. if (context == null) {
  3. return "";
  4. }
  5. DisplayMetrics dm = context.getResources().getDisplayMetrics();
  6. int widthPixels = dm.widthPixels;
  7. int heightPixels = dm.heightPixels;
  8. return widthPixels + "*" + heightPixels;
  9. }

获取国家码

  1. public static String getCountry() {
  2. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  3. if (tm == null) {
  4. return "";
  5. }
  6. return tm.getNetworkCountryIso();
  7. }

获取系统语言

  1. public static String getLanguage() {
  2. return Locale.getDefault().getLanguage();
  3. }

获取设备网络代码

  1. public static String getMccmnc() {
  2. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  3. if (tm == null) {
  4. return "";
  5. }
  6. return tm.getNetworkOperator();
  7. }

获取设备网络类型

  1. public static String getNetworkType() {
  2. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  3. if (cm == null) {
  4. return "Unknown";
  5. }
  6. NetworkInfo activeNetInfo = cm.getActiveNetworkInfo();
  7. if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
  8. return "Unknown";
  9. }
  10. NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  11. if (wifiInfo != null) {
  12. NetworkInfo.State state = wifiInfo.getState();
  13. if (state != null && state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
  14. return "WiFi状态";
  15. }
  16. }
  17. NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  18. if (networkInfo != null) {
  19. NetworkInfo.State state = networkInfo.getState();
  20. String subtypeName = networkInfo.getSubtypeName();
  21. if (state != null && state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
  22. switch (activeNetInfo.getSubtype()) {
  23. case TelephonyManager.NETWORK_TYPE_GPRS:
  24. case TelephonyManager.NETWORK_TYPE_CDMA:
  25. case TelephonyManager.NETWORK_TYPE_EDGE:
  26. case TelephonyManager.NETWORK_TYPE_1xRTT:
  27. case TelephonyManager.NETWORK_TYPE_IDEN:
  28. return "2G";
  29. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  30. case TelephonyManager.NETWORK_TYPE_UMTS:
  31. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  32. case TelephonyManager.NETWORK_TYPE_HSDPA:
  33. case TelephonyManager.NETWORK_TYPE_HSUPA:
  34. case TelephonyManager.NETWORK_TYPE_HSPA:
  35. case TelephonyManager.NETWORK_TYPE_EVDO_B:
  36. case TelephonyManager.NETWORK_TYPE_EHRPD:
  37. case TelephonyManager.NETWORK_TYPE_HSPAP:
  38. return "3G";
  39. case TelephonyManager.NETWORK_TYPE_LTE:
  40. return "4G";
  41. default:
  42. if (subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA") || subtypeName.equalsIgnoreCase("CDMA2000")) {
  43. return "3G";
  44. }
  45. return "Unknown";
  46. }
  47. }
  48. }
  49. return "Unknown";
  50. }

获取android版本

  1. public static int getAndroidVersion() {
  2. return Build.VERSION.SDK_INT;
  3. }

获取操作系统版本

  1. public static String getOsVersion() {
  2. return Build.VERSION.RELEASE;
  3. }

获取目标sdk版本号

  1. public static int getTargetSdkVer() {
  2. if (context == null) {
  3. return 0;
  4. }
  5. return context.getApplicationInfo().targetSdkVersion;
  6. }

获取应用第一次安装时间

  1. public static long getFirstInstallTime() {
  2. if (context == null) {
  3. return 0;
  4. }
  5. PackageManager pm = context.getPackageManager();
  6. PackageInfo packageInfo = null;
  7. try {
  8. packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
  9. } catch (PackageManager.NameNotFoundException e) {
  10. e.printStackTrace();
  11. }
  12. return packageInfo.firstInstallTime;
  13. }

转载于:Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..) - 码农教程 

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

闽ICP备14008679号