get both simcard operator name in dual SIM mobile

前端 未结 5 1583
不思量自难忘°
不思量自难忘° 2021-01-06 09:10

I want to know Both sim card\'s operator name when mobile is dual sim.In single SIM I got operator name programmatically But For duel SIM I can\'t although after so many sea

相关标签:
5条回答
  • 2021-01-06 09:51

    Ofcourse you can get the details of dualsim in mobiles below version 22. It is only not officially supported only after 22.

    private static String getOutput(Context context, String methodName, int slotId) {
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Class<?> telephonyClass;
        String reflectionMethod = null;
        String output = null;
        try {
            telephonyClass = Class.forName(telephony.getClass().getName());
            for (Method method : telephonyClass.getMethods()) {
                String name = method.getName();
                if (name.contains(methodName)) {
                    Class<?>[] params = method.getParameterTypes();
                    if (params.length == 1 && params[0].getName().equals("int")) {
                        reflectionMethod = name;
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (reflectionMethod != null) {
            try {
                output = getOpByReflection(telephony, reflectionMethod, slotId, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        return output;
    }
    
    private static String getOpByReflection(TelephonyManager telephony, String predictedMethodName, int slotID, boolean isPrivate) {
    
        //Log.i("Reflection", "Method: " + predictedMethodName+" "+slotID);
        String result = null;
    
        try {
    
            Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
    
            Class<?>[] parameter = new Class[1];
            parameter[0] = int.class;
            Method getSimID;
            if (slotID != -1) {
                if (isPrivate) {
                    getSimID = telephonyClass.getDeclaredMethod(predictedMethodName, parameter);
                } else {
                    getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
                }
            } else {
                if (isPrivate) {
                    getSimID = telephonyClass.getDeclaredMethod(predictedMethodName);
                } else {
                    getSimID = telephonyClass.getMethod(predictedMethodName);
                }
            }
    
            Object ob_phone;
            Object[] obParameter = new Object[1];
            obParameter[0] = slotID;
            if (getSimID != null) {
                if (slotID != -1) {
                    ob_phone = getSimID.invoke(telephony, obParameter);
                } else {
                    ob_phone = getSimID.invoke(telephony);
                }
    
                if (ob_phone != null) {
                    result = ob_phone.toString();
    
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
            return null;
        }
        //Log.i("Reflection", "Result: " + result);
        return result;
    }
    

    Use this two methods. You have to get all sim details by using Java reflection.

    Now get the detail you want by just using a single line of code.

    String optName = getOutput(context, "getCarrierName", 0);
    

    the first param is the context. second param is the method name you wanna access and the third param is the slotId. "0" means sim 1.

    All the result of this method will be string. Convert them as per your need.

    Each mobile having its own method. Like micromax is having method like "getCarrierNameGemni". Don't worry, The code I gave you will handle everything for you. If it cant get the result, it will return null. Happy coding!

    0 讨论(0)
  • 2021-01-06 09:52

    Solution is too curious and It is only work in motoe2.not in others.In some it display null or in some only display one sim operator.

    Just change this two line:

    telephonyInfo.optName1 = telephonyInfo.getOutput(
                getApplicationContext(), "SimOperatorName", 1);
        telephonyInfo.optName2 = telephonyInfo.getOutput(
                getApplicationContext(), "SimOperatorName", 2);
    

    getOutput() method is as per answer of Sujith.

    0 讨论(0)
  • 2021-01-06 10:01

    Since API version 22 its possible

    try following code for device having os greater than android 5.1

    List<SubscriptionInfo> subscriptionInfos = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
    for(int i=0; i<subscriptionInfos.size();i++)
    {
        SubscriptionInfo lsuSubscriptionInfo = subscriptionInfos.get(i);
        Log.d(TAG, "getNumber "+ lsuSubscriptionInfo.getNumber());
        Log.d(TAG, "network name : "+ lsuSubscriptionInfo.getCarrierName());
        Log.d(TAG, "getCountryIso "+ lsuSubscriptionInfo.getCountryIso());
    }
    

    for more information : Documentation: SubscriptionManager - Android Hope it helps. Peace.

    0 讨论(0)
  • 2021-01-06 10:12

    For API >=22:

    {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    
            List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
    
            if (subscriptionInfoList != null && subscriptionInfoList.size() > 0) {
                for (SubscriptionInfo info : subscriptionInfoList) {
                    String carrierName = info.getCarrierName().toString();
                    String mobileNo = info.getNumber();
                    String countyIso = info.getCountryIso();
                    int dataRoaming = info.getDataRoaming();
    
                }
    
            }
        }
    
    0 讨论(0)
  • 2021-01-06 10:13

    For any android device. This may help you. try this!

       //above 22
     if (Build.VERSION.SDK_INT > 22) {
            //for dual sim mobile
            SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);
    
            if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
             //if there are two sims in dual sim mobile
                List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
                SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
                SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);
    
                final String sim1 = simInfo.getDisplayName().toString();
                final String sim2 = simInfo1.getDisplayName().toString();
    
            }else{
             //if there is 1 sim in dual sim mobile
                TelephonyManager tManager = (TelephonyManager) getBaseContext()
                        .getSystemService(Context.TELEPHONY_SERVICE);
    
                String sim1 = tManager.getNetworkOperatorName();
    
            }
    
        }else{
            //below android version 22
                    TelephonyManager tManager = (TelephonyManager) getBaseContext()
                            .getSystemService(Context.TELEPHONY_SERVICE);
    
                    String sim1 = tManager.getNetworkOperatorName();
        }
    
    0 讨论(0)
提交回复
热议问题