android - 如何识别 sdk 级别低于 android.os.Build.VERSION_CODES.LOLLIPOP_MR1 的双卡运营商名称

标签 android telephonymanager dual-sim

我从 sdk 级别 android.os.Build.VERSION_CODES.LOLLIPOP_MR1 知道我们得到 subscriptionInfoList 作为 subscriptionManager.getActiveSubscriptionInfoList(); 通过使用它,我们将识别所有支持的 SIM 卡信息。

我需要在 android 较低版本中获得相同的内容。任何人都可以帮助我吗?

最佳答案

幸运的是,有几种 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。

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

关于android - 如何识别 sdk 级别低于 android.os.Build.VERSION_CODES.LOLLIPOP_MR1 的双卡运营商名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373191/

相关文章:

java - Android 双 SIM 卡 -> 更改网络

android - 如何在支持双卡双待的手机中选择主卡来发送短信?

安卓信号强度

java - 如何以编程方式设置 ViewPager layoutParams?

android - cordova-plugin-firebase 中的电话号码无效

android - 如何找到短小区ID?

android - 无法在 android 2.2 中检索电话号码

android - Android Studio 上的 INSTALL_PARSE_FAILED_NO_CERTIFICATES 错误

android - 使用 API key 获取任务数据

android - 使用 telephonyserivce.endcall() 在 android 中结束通话