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

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

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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 4.4 ,您可以使用以下代码来获取双 SIM 卡信息: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String simSerialNumber1 = telephonyManager.getSimSerialNumber(0); String simSerialNumber2 = telephonyManager.getSimSerialNumber(1); String subscriberId1 = telephonyManager.getSubscriberId(0); String subscriberId2 = telephonyManager.getSubscriberId(1); String line1Number1 = telephonyManager.getLine1Number(0); String line1Number2 = telephonyManager.getLine1Number(1); String networkOperatorName1 = telephonyManager.getNetworkOperatorName(0); String networkOperatorName2 = telephonyManager.getNetworkOperatorName(1); String networkCountryIso1 = telephonyManager.getNetworkCountryIso(0); String networkCountryIso2 = telephonyManager.getNetworkCountryIso(1); String simOperatorName1 = telephonyManager.getSimOperatorName(0); String simOperatorName2 = telephonyManager.getSimOperatorName(1); ``` 其,`getSimSerialNumber(int slotIndex)` 方法用于获取指定 SIM 卡的序列,`getSubscriberId(int slotIndex)` 方法用于获取指定 SIM 卡的 IMSI ,`getLine1Number(int slotIndex)` 方法用于获取指定 SIM 卡的手机码,`getNetworkOperatorName(int slotIndex)` 方法用于获取指定 SIM 卡所属的运营商名称,`getNetworkCountryIso(int slotIndex)` 方法用于获取指定 SIM 卡所属的国家代码,`getSimOperatorName(int slotIndex)` 方法用于获取指定 SIM 卡所属的 SIM 运营商名称。 需要注意的是,以上方法的 `slotIndex` 参数可以取值 0 或 1,分别代表第一张 SIM 卡和第二张 SIM 卡。如果您的设备只支持一张 SIM 卡,那么所有的方法都只能使用 `slotIndex` 参数取值为 0 的情况。 此外,获取双 SIM 卡信息还可以使用 `SubscriptionManager` 类,该类是在 Android 5.1 引入的,但也可以在 Android 4.4 使用,需要通过反射来调用。如果您需要使用 `SubscriptionManager` 类,请参考以下代码: ```java SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); List<SubscriptionInfo> subscriptionInfos = null; if (subscriptionManager != null) { subscriptionInfos = subscriptionManager.getActiveSubscriptionInfoList(); } if (subscriptionInfos != null) { for (SubscriptionInfo subscriptionInfo : subscriptionInfos) { int slotIndex = subscriptionInfo.getSimSlotIndex(); String simSerialNumber = subscriptionInfo.getIccId(); String subscriberId = subscriptionInfo.getImsi(); String line1Number = subscriptionInfo.getNumber(); String networkOperatorName = subscriptionInfo.getCarrierName().toString(); String networkCountryIso = subscriptionInfo.getCountryIso(); String simOperatorName = subscriptionInfo.getDisplayName().toString(); } } ``` 以上代码,`getActiveSubscriptionInfoList()` 方法用于获取当前设备上所有活跃的 SIM 卡信息,返回一个 `List<SubscriptionInfo>` 对象。`SubscriptionInfo` 类包含了获取 SIM 卡信息的所有方法,使用方法与上面介绍的方法相同。需要注意的是,`getActiveSubscriptionInfoList()` 方法只能在支持 `SubscriptionManager` 类的设备上使用,否则会出现 `java.lang.NoSuchMethodError` 异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值