Configure APNs on Android 9 (API level 28)

1.2k views Asked by At

I'm trying to use the new 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

p.s

I checked with dpm.getOverrideApns(deviceAdmin); and the added apn is present... And I also tried setting the setProtocol and setRoamingProtocol

Any Ideas?

1

There are 1 answers

0
Daniel On BEST ANSWER

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)

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

MCC and MNC could be fetched from TelephonyManager, getSimOperator() (getSimOperator().substring(3) and getSimOperator().substring(0, 3))