在 Android 9(API 级别 28)上配置 APN

Configure APNs on Android 9 (API level 28)

我正在尝试使用新的 APN api

代码如下所示

DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

ComponentName deviceAdmin = new ComponentName(getApplicationContext(), DeviceAdmin.class);

ApnSetting apn = (new ApnSetting.Builder())
        .setApnTypeBitmask(ApnSetting.TYPE_DEFAULT)
        .setApnName("sonme.real.apn.url")
        .setEntryName("Some Entry")
        .setCarrierEnabled(true)
        .build();

int re = dpm.addOverrideApn(deviceAdmin, apn);

dpm.setOverrideApnsEnabled(deviceAdmin, true);

但除了 APN 菜单变得不可用(锁定为管理员 - 这没关系)之外,APN 无法正常工作

p.s

我检查了 dpm.getOverrideApns(deviceAdmin);,添加的 apn 存在... 而且我还尝试设置 setProtocolsetRoamingProtocol

有什么想法吗?

终于知道少了什么

似乎在使用 API 添加 apns 时,您必须明确指定 setProtocolsetRoamingProtocolsetOperatorNumeric,这是必须的,它由 Telephony.Carriers.MCC + Telephony.Carriers.MNC(在我的例子中,我不得不用前导零填充 MNC)

ApnSetting apn = (new ApnSetting.Builder())
        .setApnTypeBitmask(ApnSetting.TYPE_DEFAULT)
        .setApnName("net.hotm")
        .setEntryName("HOT")
        .setCarrierEnabled(true) // enable it
        .setOperatorNumeric("425" + "07") // this is a must its consists from Telephony.Carriers.MCC + Telephony.Carriers.MNC, In my case, I had to pad the MNC with a leading zero
        .setProtocol(ApnSetting.PROTOCOL_IPV4V6) // this is a must
        .setRoamingProtocol(ApnSetting.PROTOCOL_IPV4V6) // this is a must
        .build();

int re = dpm.addOverrideApn(deviceAdmin, apn);

currApns =  dpm.getOverrideApns(deviceAdmin);

dpm.setOverrideApnsEnabled(deviceAdmin, true);

p.s

可以从 TelephonyManager, getSimOperator() (getSimOperator().substring(3) and getSimOperator().substring(0, 3))

中获取 MCC 和 MNC