【问题标题】:how to get both sim serial number below lollipop 5.0如何获得棒棒糖5.0以下的两个sim序列号
【发布时间】:2017-09-16 09:52:40
【问题描述】:

我尝试下面的代码,但两者都返回 null。

TelephonyManager telephonyInfo = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

telephonyInfo.serialSIM1 = telephonyManager.getSimSerialNumber();
        telephonyInfo.serialSIM2 = null;

        try {
            telephonyInfo.serialSIM1 = getSerialBySlot(context, "getSimSerialNumberGemini", 0);
            telephonyInfo.serialSIM2 = getSerialBySlot(context, "getSimSerialNumberGemini", 1);
        } catch (GeminiMethodNotFoundException e) {

            e.printStackTrace();

            try {
                telephonyInfo.serialSIM1 = getSerialBySlot(context, "getSimSerialNumber", 0);
                telephonyInfo.serialSIM2 = getSerialBySlot(context, "getSimSerialNumber", 1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }

private static String getSerialBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    String lsSerialNumber = null;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try{

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimID.invoke(telephony, obParameter);

        if(ob_phone != null){
            lsSerialNumber = ob_phone.toString();

        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return lsSerialNumber;
}


private static class GeminiMethodNotFoundException extends Exception {

    private static final long serialVersionUID = -996812356902545308L;

    public GeminiMethodNotFoundException(String info) {
        super(info);
    }
}

那就用吧。

String getSerialNumber1 = telephonyInfo.getSerialSIM1();
String getSerialNumber2 = telephonyInfo.getSerialSIM2();

Log.i(TAG, "state getSerialNumber1 : " + getSerialNumber1);
Log.i(TAG, "state getSerialNumber2 : " + getSerialNumber2);

下面是logcat中得到的日志

09-16 07:20:07.042 6973-6973/app.myApp I/SimChangeReceiver: state getSerialNumber1 : null
09-16 07:20:07.042 6973-6973/app.myApp I/SimChangeReceiver: state getSerialNumber2 : null

我是否可以获取 IMEI 和双卡。 我搜索了相关的问题,但我没有找到任何其他方法。

【问题讨论】:

标签: android sim-card receiver


【解决方案1】:

试试这个我没有测试过,但它可能会帮助你。

您可以使用SubscriptionManager,而不是使用getSimSerialNumber。

// Requires, android.permission.READ_PHONE_STATE
SubscriptionManager sm = SubscriptionManager.from(mContext);

// it returns a list with a SubscriptionInfo instance for each simcard
// there is other methods to retrieve SubscriptionInfos (see [2])
        List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();

// getting first SubscriptionInfo
        SubscriptionInfo si = sis.get(0);

// getting iccId
        String iccId = si.getIccId();

【讨论】:

  • 这可以从 5.1 Lollipop 开始使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多