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;
        }
    }
}
相关推荐
Dingdangr4 小时前
Android中的Intent的作用
android
技术无疆4 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
GEEKVIP4 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
Jouzzy11 小时前
【Android安全】Ubuntu 16.04安装GDB和GEF
android·ubuntu·gdb
极客先躯11 小时前
java和kotlin 可以同时运行吗
android·java·开发语言·kotlin·同时运行
Good_tea_h13 小时前
Android中的单例模式
android·单例模式
计算机源码社18 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
丶白泽19 小时前
重修设计模式-结构型-门面模式
android
晨春计20 小时前
【git】
android·linux·git
标标大人21 小时前
c语言中的局部跳转以及全局跳转
android·c语言·开发语言