繁体   English   中英

如何从双卡手机Android获取运营商名称?

[英]How to get carrier name from dual sim phone Android?

我可以从决斗SIM手机获得运营商名称。

我使用了以下代码,但它仅适用于单个SIM卡手机。

TelephonyManager telephonyManager = ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));

幸运的是,在搜索获取网络运营商的方式时,我发现了几种原生解决方案。

对于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。 要检查手机是双/三/多SIM卡:

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

使用“SubscriptionManager”我们可以知道android mobile的双sim卡的运营商名称。

SubscriptionManager subscriptionManager=(SubscriptionManager)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();

               }

            }

由于android不支持至少低于API级别22的多个sim。

从API 22开始,您可以使用SubscriptionManager的方法getActiveSubscriptionInfoList()检查多个SIM。 有关https://developer.android.com/reference/android/telephony/SubscriptionManager.html#getActiveSubscriptionInfoList%28%29的更多详细信息。

有关详细信息,请参阅以下链接检测双卡Android手机中两张SIM卡的状态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM