获取双卡Android手机的运营商详情

3

我正在开发一个应用程序,需要获取用户SIM卡的详细信息,包括电话号码、IMEI号码和运营商。目前,我已经通过这个答案获得了他的IMEI号码,但不确定他的设备是单卡还是双卡。如何获取他两张SIM卡的号码和运营商名称。

4个回答

3

试一试,这对我有效:

TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();

Android SDK不支持多个SIM卡。我们只能获取当前网络运营商或SIM卡运营商的名称,如果没有SIM卡,则结果将为“”。这将仅适用于主要运营商。 - Ana Llera
3
从API 22开始,现在你可以检查多个SIM卡。请查看Android文档 - Bugs Happen

3

幸运的是,有几个本地解决方案。希望这能帮助到某些人。

对于API版本号 >=17:

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

// Get information about all radio modules on device board
// and check what you need by calling #getCellIdentity.

final List<CellInfo> allCellInfo = manager.getAllCellInfo();
for (CellInfo cellInfo : allCellInfo) {
    if (cellInfo instanceof CellInfoGsm) {
        CellIdentityGsm cellIdentity = ((CellInfoGsm) cellInfo).getCellIdentity();
        //TODO Use cellIdentity to check MCC/MNC code, for instance.
    } else if (cellInfo instanceof CellInfoWcdma) {
        CellIdentityWcdma cellIdentity = ((CellInfoWcdma) cellInfo).getCellIdentity();
    } else if (cellInfo instanceof CellInfoLte) {
        CellIdentityLte cellIdentity = ((CellInfoLte) cellInfo).getCellIdentity();
    } else if (cellInfo instanceof CellInfoCdma) {
        CellIdentityCdma cellIdentity = ((CellInfoCdma) cellInfo).getCellIdentity();
    } 
}

在 AndroidManifest 文件中添加权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>

要获取网络运营商,您可以检查MCC和MNC代码: 对于API >= 22:
final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
final List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
for (SubscriptionInfo subscriptionInfo : activeSubscriptionInfoList) {
    final CharSequence carrierName = subscriptionInfo.getCarrierName();
    final CharSequence displayName = subscriptionInfo.getDisplayName();
    final int mcc = subscriptionInfo.getMcc();
    final int mnc = subscriptionInfo.getMnc();
    final String subscriptionInfoNumber = subscriptionInfo.getNumber();
}

对于API >=23版本。如果只是检查手机是否具有双/三/多卡功能,请使用以下方法:

TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager.getPhoneCount() == 2) {
    // Dual sim
}

似乎对于SubscriptionManager,您需要另一个危险权限READ_PHONE_STATE。此外,第一种解决方案将返回的不是SIM,而是塔信息。 - sandrstar

1
尝试这里:
private List<String> getNetworkOperator(final Context context) {
    // Get System TELEPHONY service reference
    List<String> carrierNames = new ArrayList<>();
    try {
        final String permission = Manifest.permission.READ_PHONE_STATE;
        if ( (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) && (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED) ){
            final List<SubscriptionInfo> subscriptionInfos = SubscriptionManager.from(context).getActiveSubscriptionInfoList();
            for (int i = 0; i < subscriptionInfos.size(); i++) {
                carrierNames.add(subscriptionInfos.get(i).getCarrierName().toString());
            }

        } else {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            // Get carrier name (Network Operator Name)
            carrierNames.add(telephonyManager.getNetworkOperatorName());

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return carrierNames;
}

"SubscriptionManager.from(context).getActiveSubscriptionInfoList()" 在单卡和双卡环境下都能正常工作,而 "telephonyManager.getNetworkOperatorName()" 在双卡环境下往往只返回一个运营商名称。 - Makari Kevin

1
        // Use this code, it will provide all the info realated both sim card 

        Required Permission:
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

            if (getSimInfo(context).get(0) != null) {  // SIM 1
                               getSimInfo(context).get(0).getMcc()); 
                               getSimInfo(context).get(0).getMnc());
                            } else {
                               Log.d("Sim card", "Sim card not available");
                            }
                            if (getSimInfo(context).get(1) != null) { // SIM 2
                                getSimInfo(context).get(1).getMcc());
                                getSimInfo(context).get(1).getMnc());
                            } else {
                               Log.d("Sim card", "Sim card not available");
                            }
                            if (getNetworkOperator(context).get(0) != null) { // SIM 1
     (String)getSimInfo(context).get(0).getCarrierName());
                            } else {
                               Log.d("Sim card", "Sim card not available");
                            }
                            if (getNetworkOperator(context).get(1) != null) {// SIM 2 
 (String)getSimInfo(context).get(1).getCarrierName());
                            } else {
                              Log.d("Sim card", "Sim card not available");
                            }

        // getSimInfo
        public List<SubscriptionInfo> getSimInfo(Context context) {
                SubscriptionManager subManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
                List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
                subscriptionInfoList = subManager.getActiveSubscriptionInfoList();
                Log.d("LIST LIST", subscriptionInfoList.toString());
                if (subscriptionInfoList == null) {
                    Toast.makeText(context, "address not found", Toast.LENGTH_SHORT).show();
                }
                return subscriptionInfoList;
            }

            // getNetworkOperator 
            public List<String> getNetworkOperator(final Context context) {
                // Get System TELEPHONY service reference
                List<String> carrierNames = new ArrayList<>();
                try {
                    final String permission = android.Manifest.permission.READ_PHONE_STATE;
                    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) && (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED)) {
                        final List<SubscriptionInfo> subscriptionInfos = SubscriptionManager.from(context).getActiveSubscriptionInfoList();
                        for (int i = 0; i < subscriptionInfos.size(); i++) {
                            carrierNames.add(subscriptionInfos.get(i).getCarrierName().toString());
                        }

                    } else {
                        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                        // Get carrier name (Network Operator Name)
                        carrierNames.add(telephonyManager.getNetworkOperatorName());
            enter code here
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return carrierNames;
            }

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