2016-03-02 69 views
0

如果手機是雙SIM卡或相鄰網絡運營商名稱的整個列表,我需要獲取網絡運營商名稱。當我谷歌搜索我發現網絡運營商名稱是這樣的獲得雙網手機編程網絡運營商的完整列表

TelephonyManager tManager = (TelephonyManager) getBaseContext() 
     .getSystemService(Context.TELEPHONY_SERVICE); 

// Get carrier name (Network Operator Name) 
String carrierName = tManager.getNetworkOperatorName(); 

但這不會爲我工作。我怎樣才能做到這一點。如果不建議我一些解決方案。謝謝

回答

0

幸運的是,有幾種原生解決方案。

對於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 
}