Get "No permission to write APN settings" error on android 6

8.5k views Asked by At

My application works fine on Android 5.0 and below, but when I tried to run it on a device with Android 6.0 or higher, I got this error:

Writing exception to parcel
                                                 java.lang.SecurityException: No permission to write APN settings
                                                     at com.android.providers.telephony.TelephonyProvider.checkPermission(TelephonyProvider.java:4058)
                                                     at com.android.providers.telephony.TelephonyProvider.query(TelephonyProvider.java:2724)
                                                     at android.content.ContentProvider.query(ContentProvider.java:1058)
                                                     at android.content.ContentProvider$Transport.query(ContentProvider.java:245)
                                                     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                     at android.os.Binder.execTransact(Binder.java:453)

Any help would be appreciated.

1

There are 1 answers

0
Amir_P On BEST ANSWER

Maybe you're not getting permission in runtime. so do this

    int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_APN_SETTINGS);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        //requesting permission
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_APN_SETTINGS}, 1);
    } else {
        //permission is granted and you can change APN settings
    }

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //premission granted by user
            } else {
                //permission denied by user
            }
        }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}