在双卡手机中获取两个SIM卡的运营商名称

8

我希望在手机是双卡时,能够知道两张sim卡的运营商名称。对于单卡,我可以通过编程方式获得运营商名称,但是对于双卡,尽管经过了很多搜索和尝试,我仍然无法获得。

如果我在双卡手机上运行我的应用程序,那么我可以在我的应用程序中获取两个sim卡的运营商名称,例如:Idea、Vodafone。

编辑:

是否有人知道如何在拥有IMEI号码的情况下获取SIM卡运营商名称。

代码:

public class MainActivity extends Activity {

Button btnOne, btnTwo;
TextView tvInfo;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnOne = (Button) findViewById(R.id.btnOne);
    btnTwo = (Button) findViewById(R.id.btnTwo);
    tvInfo = (TextView) findViewById(R.id.tvInfo);

    TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);

    boolean isDualSIM = telephonyInfo.isDualSIM();
    boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
    boolean isSIM2Ready = telephonyInfo.isSIM2Ready();

    TelephonyManager manager = (TelephonyManager) getApplicationContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    try {
        telephonyInfo.imsiSIM1 = telephonyInfo.getDeviceIdBySlot(context,
                "getSimSerialNumberGemini", 0);
        telephonyInfo.imsiSIM2 = telephonyInfo.getDeviceIdBySlot(context,
                "getSimSerialNumberGemini", 1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String number = manager.getLine1Number();

    String optName1 = getOutput(getApplicationContext(), "getCarrierName", 0);
    String optName2 = getOutput(getApplicationContext(), "getCarrierName", 1);

    final String carrierName = manager.getSimOperatorName();
    tvInfo.setText(" " + isDualSIM + " " + optName1 + " " + optName2 + " "
            + telephonyInfo.imsiSIM1 + " " + telephonyInfo.imsiSIM2 + " "
            + number + " " + isSIM1Ready + " " + isSIM2Ready);

    btnOne.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(
                    Settings.ACTION_ACCESSIBILITY_SETTINGS);
            startActivity(intent);

        }
    });

    btnTwo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (carrierName.equalsIgnoreCase("TATA DOCOMO")
                    || carrierName.contains("DOCOMO")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "111" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            } else if (carrierName.equalsIgnoreCase("!dea")
                    || carrierName.contains("idea")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "121" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            } else if (carrierName.equalsIgnoreCase("AIRTEL")
                    || carrierName.contains("airtel")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "123" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            }

        }
    });

}

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();
        // Log.i("Reflection", "Result: " +  e.printStackTrace());
         return null;
    }

    return result;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
5个回答

11

从API版本22开始,设备的操作系统大于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());
}

更多信息请参考:文档:SubscriptionManager - Android 希望能对您有所帮助。祝和平。


3
@ibtehaz说的是5.1而不是5.2。 - Prashant
@Prashant,谢谢。我的错。 - O_o
请检查以下链接:http://stackoverflow.com/questions/31468108/how-can-i-get-the-service-provider-name-of-other-phone-numbers-in-contacts-list - D.J
@KrishnaJ,这里有一个链接 > https://dev59.com/02Yr5IYBdhLWcg3wssKP#18782517 < 他们使用了Telephony Manager类来处理两张SIM卡。我不确定它是否有效。 - O_o
两个链接都无法为两张SIM卡提供运营商名称。我已经尝试过了。 - KrishnaJ
显示剩余8条评论

5

针对 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();

            }

        }
    }

4

适用于任何安卓设备。这可能会对你有所帮助。试试这个!

   //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();
    }

3
解决方案太过神奇,只在motoe2上工作,其他设备无法正常运行。在某些设备上显示为空值,在某些设备上仅显示一个SIM卡运营商。
只需更改这两行代码:
telephonyInfo.optName1 = telephonyInfo.getOutput(
            getApplicationContext(), "SimOperatorName", 1);
    telephonyInfo.optName2 = telephonyInfo.getOutput(
            getApplicationContext(), "SimOperatorName", 2);

getOutput()方法按照Sujith的回答。


3
当然,您可以在版本低于22的手机中获取双卡详细信息。只有在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;
}

使用这两种方法。您需要使用Java反射获取所有SIM卡详细信息。

现在,只需使用一行代码即可获取所需的详细信息。

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

第一个参数是上下文。 第二个参数是您想要访问的方法名,第三个参数是slotId。 "0"表示sim1

此方法的所有结果都将是字符串。根据您的需要进行转换。

每个手机都有自己的方法。例如micromax具有名为“getCarrierNameGemni”的方法。别担心,我给你的代码将为您处理一切。如果无法获取结果,它将返回null。愉快的编码!


你使用的是哪款手机? - Sujith Niraikulathan
Moto E2 第二代 - KrishnaJ
你有任何日志吗?堆栈跟踪? - Sujith Niraikulathan
联想K3 Note中也有null。 - KrishnaJ
你是这样调用它的吗:"String optName = getOutput(context, "getCarrierNameGemni", 0);"? - Sujith Niraikulathan
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接