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

时间:2019-12-26 09:07:21

标签: android device-policy-manager android-enterprise android-enterprise-features

我正在尝试使用新的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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最后弄清楚丢失了什么

看来,使用API​​添加APN时,您必须明确指定setProtocolsetRoamingProtocolsetRoamingProtocol,这是必须的,它由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
        .setRoamingProtocol("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