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

标签 java android

我可以从双卡手机获取运营商名称。

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

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

最佳答案

幸运的是,在寻找获得网络运营商的方式时,我发现了几种 native 解决方案。

对于 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
}

关于java - 如何从双卡手机 Android 获取运营商名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714656/

相关文章:

android - 将 child 放入 include 标签

android - 如何在Android Studio中找到gradle插件的所有可用属性和方法?

java - 未找到证书路径的 SSLHandshakeException 信任 anchor Android HTTPS

java - Jetty 服务器未启动 : Unable to establish loopback connection

java - 支持在 HttpUnit 中发布 XML?

java - 为什么这个模式匹配代码不起作用?

java - Facebook SDK v3.5.2 - 从封闭 session 请求权限

Android:从 Url 显示在 Webview 的图像具有高质量损失

java8 一个 map 列表,按键减少

java - 如何在OSMDroid上实现可拖动的ExtendOverlayItem?