简体   繁体   English

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

[英]Configure APNs on Android 9 (API level 28)

I'm trying to use the new APN api我正在尝试使用新的 APN api

The code looks like this代码看起来像这样

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);

But except the fact that the APN menu becomes unavailable (locked to admin - which is OK) the APN is not working但除了 APN 菜单变得不可用(锁定到管理员 - 这没关系)之外,APN 无法正常工作

ps ps

I checked with dpm.getOverrideApns(deviceAdmin);我检查了dpm.getOverrideApns(deviceAdmin); and the added apn is present... And I also tried setting the setProtocol and setRoamingProtocol并且添加的 apn 存在......我还尝试设置setProtocolsetRoamingProtocol

Any Ideas?有任何想法吗?

Finally figured out what was missing终于知道少了什么

It appears that when adding apns using the API you must explicitly specify the setProtocol , setRoamingProtocol and the setOperatorNumeric ,its a must and its consists from Telephony.Carriers.MCC + Telephony.Carriers.MNC (in my case, I had to pad the MNC with a leading zero)似乎在使用 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);

ps ps

MCC and MNC could be fetched from TelephonyManager, getSimOperator() ( getSimOperator().substring(3) and getSimOperator().substring(0, 3) ) MCC 和 MNC 可以从TelephonyManager, getSimOperator()getSimOperator().substring(3) and getSimOperator().substring(0, 3) )获取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM