• Android中获取手机SIM卡的各种信息


     通过以下工具类方法可以获取到手机SIM的各种信息数据!!!

    1. package com.utils;
    2. import android.telephony.TelephonyManager;
    3. import com.baidu.platform.comapi.map.E;
    4. import org.json.JSONArray;
    5. import org.json.JSONObject;
    6. import java.lang.reflect.Method;
    7. import java.util.ArrayList;
    8. import java.util.HashMap;
    9. import java.util.List;
    10. import java.util.Map;
    11. /**
    12. * 使用方法
    13. * TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    14. * SONArray simList = SimUtils.getAllSimInfo(tel);
    15. */
    16. public class SimUtils {
    17. public static JSONArray getAllSimInfo(TelephonyManager tel) throws Exception {
    18. Class clazz = tel.getClass();
    19. //获取能够进行反射的字段
    20. List list = new ArrayList<>();
    21. Map listIgnore = new HashMap<>();
    22. Method[] methods = clazz.getDeclaredMethods();
    23. for (Method method : methods) {
    24. String name = method.getName();
    25. if (!name.startsWith("get"))
    26. continue;
    27. if (listIgnore.get(name) != null)
    28. continue;
    29. listIgnore.put(name, 0);
    30. Method m1 = null;
    31. Method m2 = null;
    32. Method m3 = null;
    33. try {
    34. m1 = clazz.getDeclaredMethod(name);
    35. } catch (Exception e) {
    36. }
    37. try {
    38. m2 = clazz.getDeclaredMethod(name, int.class);
    39. } catch (Exception e) {
    40. }
    41. try {
    42. m3 = clazz.getDeclaredMethod(name, long.class);
    43. } catch (Exception e) {
    44. }
    45. if (m1 != null && ((m2 == null && m3 != null) || (m2 != null && m3 == null))) {
    46. Class c1 = m1.getReturnType();
    47. Class c2 = m2 == null ? null : m2.getReturnType();
    48. Class c3 = m3 == null ? null : m3.getReturnType();
    49. if (m2 == null) {
    50. if (c1 == null || c1 != c3)
    51. continue;
    52. } else {
    53. if (c1 == null || c1 != c2)
    54. continue;
    55. }
    56. EMethod item = new EMethod(name, m2 == null ? 1 : 0, c1);
    57. list.add(item);
    58. }
    59. }
    60. listIgnore.clear();
    61. JSONArray array = new JSONArray();
    62. for (int i = 0; i < 10; i++) {
    63. JSONObject json = new JSONObject();
    64. for (EMethod em : list) {
    65. Method method = null;
    66. Object param = null;
    67. if (em.type == 0) {
    68. method = clazz.getDeclaredMethod(em.name, int.class);
    69. param = i;
    70. } else {
    71. method = clazz.getDeclaredMethod(em.name, long.class);
    72. param = new Long(i);
    73. }
    74. if (!method.isAccessible())
    75. method.setAccessible(true);
    76. String name = em.name.substring(3);
    77. Object value = null;
    78. try {
    79. value = method.invoke(tel, param);
    80. } catch (Exception e) {
    81. //前面已经对private设置了可访问,有些仍是会报错,就无论这个了
    82. continue;
    83. }
    84. json.put(name, value);
    85. }
    86. if (json.optInt("SimState") == TelephonyManager.SIM_STATE_UNKNOWN || json.optInt("SimState") == TelephonyManager.SIM_STATE_ABSENT)
    87. continue;
    88. String imsi = json.optString("SubscriberId");
    89. if (imsi == null || imsi.length() == 0)
    90. continue;
    91. //根据imsi去重
    92. boolean repeact = false;
    93. for (int j = 0; j < array.length(); j++) {
    94. if (imsi.equals(array.optJSONObject(j).optString("SubscriberId"))) {
    95. repeact = true;
    96. break;
    97. }
    98. }
    99. if (repeact)
    100. continue;
    101. array.put(json);
    102. }
    103. return array;
    104. }
    105. static class EMethod {
    106. public String name;
    107. public int type;//0为int,1为long
    108. public Class returnType;//返回类型
    109. public EMethod(String name, int type, Class returnType) {
    110. this.name = name;
    111. this.type = type;
    112. this.returnType = returnType;
    113. }
    114. }
    115. }

  • 相关阅读:
    BioVendor sRAGE Elisa试剂盒化学性质和技术研究
    2023年Java毕业设计题目如何选题?Java毕业设计选题大全
    解密Prompt系列21. LLM Agent之再谈RAG的召回信息密度和质量
    计算就是创造力!阿里云与FIRST影展设立“无影创作奖”
    MXProxyPool: 动态爬虫IP池(抓取、存储、测试)
    细粒度图像分类论文研读-2014
    java基础-第4章-面向对象(二)
    【Axure高保真原型】人物卡片多条件搜索案例
    java.lang.Float类下toString(float f)方法具有什么功能呢?
    为什么做生意可以让双方生活的更好?
  • 原文地址:https://blog.csdn.net/aidou1314/article/details/132581051