android 获取设备信息

举报
再见孙悟空_ 发表于 2022/01/12 23:15:11 2022/01/12
【摘要】 //手机号码 public static String getLine1Number(Context context) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);...


  
  1. //手机号码
  2. public static String getLine1Number(Context context) {
  3. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  4. if (tm == null) {
  5. return "";
  6. }
  7. return "" + tm.getLine1Number();
  8. }
  9. //deviceId
  10. public static String getDeviceId(Context context) {
  11. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  12. if (tm == null) {
  13. return "";
  14. }
  15. return "" + tm.getDeviceId();
  16. }
  17. //运营商名称
  18. public static String getNetworkOperatorName(Context context) {
  19. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  20. if (tm == null) {
  21. return "";
  22. }
  23. return "" + tm.getNetworkOperatorName();
  24. }
  25. //sim卡序列号
  26. public static String getSimSerialNumber(Context context) {
  27. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  28. if (tm == null) {
  29. return "";
  30. }
  31. return "" + tm.getSimSerialNumber();
  32. }
  33. //IMSI
  34. public static String getSubscriberId(Context context) {
  35. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  36. if (tm == null) {
  37. return "";
  38. }
  39. return "" + tm.getSubscriberId();
  40. }
  41. //sim卡所在国家
  42. public static String getNetworkCountryIso(Context context) {
  43. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  44. if (tm == null) {
  45. return "";
  46. }
  47. return "" + tm.getNetworkCountryIso();
  48. }
  49. //运营商编号。
  50. public static String getNetworkOperator(Context context) {
  51. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  52. if (tm == null) {
  53. return "";
  54. }
  55. return tm.getNetworkOperator();
  56. }
  57. //android 获取当前手机型号
  58. public static String getPhoneModel(Context context) {
  59. Build bd = new Build();
  60. return bd.MODEL;
  61. }
  62. //android 获取当前手机品牌
  63. public static String getPhoneProduct(Context context) {
  64. Build bd = new Build();
  65. return bd.PRODUCT;
  66. }
  67. //android 获取屏幕分辩率
  68. public static String getMetrics(Context context) {
  69. DisplayMetrics dm = new DisplayMetrics();
  70. int h = dm.heightPixels;
  71. int w = dm.widthPixels;
  72. return h+ "*" +w;
  73. }
  74. //android获取当前时区
  75. public static String getTimeZone(Context context) {
  76. TimeZone tz = TimeZone.getDefault();
  77. String s = tz.getID();
  78. System.out.println(s);
  79. return s;
  80. }
  81. //android获取当前日期时间
  82. public static String getDateAndTime(Context context) {
  83. SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
  84. Date curDate = new Date(System.currentTimeMillis());//获取当前时间
  85. String str = formatter.format(curDate);
  86. return str;
  87. }
  88. //获取手机系统语言 0中文简体 1其它
  89. public static String getLanguage(Context context) {
  90. Locale locale = context.getResources().getConfiguration().locale;
  91. String language = locale.getLanguage();
  92. if (language.endsWith("zh"))
  93. return "0";
  94. else
  95. return "1";
  96. }

  
  1. /**
  2. * 获取网络类型
  3. */
  4. public static int getNetWorkType(Context context) {
  5. ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  6. NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  7. if (networkInfo != null && networkInfo.isConnected()) {
  8. String type = networkInfo.getTypeName();
  9. if (type.equalsIgnoreCase("WIFI")) {
  10. return AVConstants.NETTYPE_WIFI;
  11. } else if (type.equalsIgnoreCase("MOBILE")) {
  12. NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  13. if (mobileInfo != null) {
  14. switch (mobileInfo.getType()) {
  15. case ConnectivityManager.TYPE_MOBILE:// 手机网络
  16. switch (mobileInfo.getSubtype()) {
  17. case TelephonyManager.NETWORK_TYPE_UMTS:
  18. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  19. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  20. case TelephonyManager.NETWORK_TYPE_HSDPA:
  21. case TelephonyManager.NETWORK_TYPE_HSUPA:
  22. case TelephonyManager.NETWORK_TYPE_HSPA:
  23. case TelephonyManager.NETWORK_TYPE_EVDO_B:
  24. case TelephonyManager.NETWORK_TYPE_EHRPD:
  25. case TelephonyManager.NETWORK_TYPE_HSPAP:
  26. return AVConstants.NETTYPE_3G;
  27. case TelephonyManager.NETWORK_TYPE_CDMA:
  28. case TelephonyManager.NETWORK_TYPE_GPRS:
  29. case TelephonyManager.NETWORK_TYPE_EDGE:
  30. case TelephonyManager.NETWORK_TYPE_1xRTT:
  31. case TelephonyManager.NETWORK_TYPE_IDEN:
  32. return AVConstants.NETTYPE_2G;
  33. case TelephonyManager.NETWORK_TYPE_LTE:
  34. return AVConstants.NETTYPE_4G;
  35. default:
  36. return AVConstants.NETTYPE_NONE;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. return AVConstants.NETTYPE_NONE;
  43. }
  44. /*
  45. * 网络连接是否可用
  46. */
  47. public static boolean isNetworkAvailable(Context context) {
  48. ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  49. if (connectivity != null) {
  50. NetworkInfo info = connectivity.getActiveNetworkInfo();
  51. if (info != null && info.isConnected()) {
  52. // 当前网络是连接的
  53. if (info.getState() == NetworkInfo.State.CONNECTED) {
  54. // 当前所连接的网络可用
  55. return true;
  56. }
  57. }
  58. }
  59. return false;
  60. }

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/51690035

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200