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

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

java 复制代码
package com.utils;
import android.telephony.TelephonyManager;
import com.baidu.platform.comapi.map.E;
import org.json.JSONArray;
import org.json.JSONObject;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用方法
 * TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
 * SONArray simList = SimUtils.getAllSimInfo(tel);
 */
public class SimUtils {
    public static JSONArray getAllSimInfo(TelephonyManager tel) throws Exception {
        Class clazz = tel.getClass();
        //获取能够进行反射的字段
        List<EMethod> list = new ArrayList<>();
        Map listIgnore = new HashMap<>();
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            String name = method.getName();
            if (!name.startsWith("get"))
                continue;
            if (listIgnore.get(name) != null)
                continue;
            listIgnore.put(name, 0);
            Method m1 = null;
            Method m2 = null;
            Method m3 = null;

            try {
                m1 = clazz.getDeclaredMethod(name);
            } catch (Exception e) {
            }
            try {
                m2 = clazz.getDeclaredMethod(name, int.class);
            } catch (Exception e) {
            }
            try {
                m3 = clazz.getDeclaredMethod(name, long.class);
            } catch (Exception e) {
            }
            if (m1 != null && ((m2 == null && m3 != null) || (m2 != null && m3 == null))) {
                Class c1 = m1.getReturnType();
                Class c2 = m2 == null ? null : m2.getReturnType();
                Class c3 = m3 == null ? null : m3.getReturnType();
                if (m2 == null) {
                    if (c1 == null || c1 != c3)
                        continue;
                } else {
                    if (c1 == null || c1 != c2)
                        continue;
                }
                EMethod item = new EMethod(name, m2 == null ? 1 : 0, c1);
                list.add(item);
            }
        }
        listIgnore.clear();
        JSONArray array = new JSONArray();
        for (int i = 0; i < 10; i++) {
            JSONObject json = new JSONObject();
            for (EMethod em : list) {
                Method method = null;
                Object param = null;
                if (em.type == 0) {
                    method = clazz.getDeclaredMethod(em.name, int.class);
                    param = i;
                } else {
                    method = clazz.getDeclaredMethod(em.name, long.class);
                    param = new Long(i);
                }
                if (!method.isAccessible())
                    method.setAccessible(true);
                String name = em.name.substring(3);
                Object value = null;
                try {
                    value = method.invoke(tel, param);
                } catch (Exception e) {
                    //前面已经对private设置了可访问,有些仍是会报错,就无论这个了
                    continue;
                }
                json.put(name, value);
            }
            if (json.optInt("SimState") == TelephonyManager.SIM_STATE_UNKNOWN || json.optInt("SimState") == TelephonyManager.SIM_STATE_ABSENT)
                continue;
            String imsi = json.optString("SubscriberId");
            if (imsi == null || imsi.length() == 0)
                continue;

            //根据imsi去重
            boolean repeact = false;
            for (int j = 0; j < array.length(); j++) {
                if (imsi.equals(array.optJSONObject(j).optString("SubscriberId"))) {
                    repeact = true;
                    break;
                }
            }
            if (repeact)
                continue;
            array.put(json);
        }
        return array;
    }
    static class EMethod {
        public String name;
        public int type;//0为int,1为long
        public Class returnType;//返回类型
        public EMethod(String name, int type, Class returnType) {
            this.name = name;
            this.type = type;
            this.returnType = returnType;
        }
    }
}
相关推荐
长亭外的少年1 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿3 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神4 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛4 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法5 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter6 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快7 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl8 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5
麦田里的守望者江8 小时前
KMP 中的 expect 和 actual 声明
android·ios·kotlin
Dnelic-8 小时前
解决 Android 单元测试 No tests found for given includes:
android·junit·单元测试·问题记录·自学笔记