Sanjeev Sharma
Sanjeev Sharma

Reputation: 31

java.lang.SecurityException: No permission to write APN settings

In my android application, i want to change the device APN with android code. but it gives the Security Exception. i did many searches on google but didn't get any answer. if anyone know about this security exception solution then help me.

Intent intent = new Intent(android.provider.Settings.ACTION_APN_SETTINGS);
                addApn(intent,MainActivity.this);
                startActivityForResult(intent,1);

i used this method to post static data on APN Settings.

 private void addApn(Intent intent, Context context) {
        final String apn = intent.getStringExtra("www.airtelgprs.com");
        final String name = intent.getStringExtra("airtel");
        final String type = intent.getStringExtra("ope");
        final String proxy = intent.getStringExtra("12.25.25");
        final int mnc = intent.getIntExtra("mnc", 6);
        final int mcc = intent.getIntExtra("mcc", 724);
        final String user = intent.getStringExtra("SAnjeev");
        final String password = intent.getStringExtra("123456789");
        final String server = intent.getStringExtra("mera server");

        final ContentResolver cr = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(Telephony.Carriers.APN, apn);
        values.put(Telephony.Carriers.NAME, name);
        values.put(Telephony.Carriers.TYPE, type);
        values.put(Telephony.Carriers.PROXY, proxy);
        values.put(Telephony.Carriers.MNC, mnc);
        values.put(Telephony.Carriers.MCC, mcc);
        values.put(Telephony.Carriers.USER, user);
        values.put(Telephony.Carriers.PASSWORD, password);
        values.put(Telephony.Carriers.SERVER, server);
        cr.insert(Telephony.Carriers.CONTENT_URI, values);

    }



      <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"
            tools:ignore="ProtectedPermissions" />


java.lang.SecurityException: No permission to write APN settings
        at android.os.Parcel.readException(Parcel.java:1620)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
        at android.content.ContentResolver.query(ContentResolver.java:493)
        at android.content.ContentResolver.query(ContentResolver.java:435)

Upvotes: 1

Views: 13238

Answers (4)

Hossein
Hossein

Reputation: 324

I resolved this problem and tested it on Android 7.1.1.
You must convert your program as a System App, so follow the below step:

1- Generate App.apk
2- run emulator as writable-system(emulator file exist in SDK_HOME/tools)
       .\emulator.exe -avd {EMULATOR NAME} -writable-system
3- to see list of emulator name run this command
     .\emulator.exe -list-avds
4- run adb shell as root
    adb root
    adb shell
5- run this command
    mount -o rw,remount /system
6- copy App.apk to /system/app and /system/priv-app
7- reboot emulator

Upvotes: 0

Rafael Sermenho
Rafael Sermenho

Reputation: 23

According to the documentation, this permission is not granted to 3rd party apps.

https://developer.android.com/reference/android/Manifest.permission#WRITE_APN_SETTINGS

Upvotes: 1

Siddhesh Golatkar
Siddhesh Golatkar

Reputation: 295

the APN write permission is granted to system app only. you are facing this error because of permissions.

Upvotes: 2

&#211;scar
&#211;scar

Reputation: 726

Maybe you're not getting permission in runtime. Try with this:

@Override
    protected void onResume() {
        super.onResume();

        int storagePermissionGranted = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_APN_SETTINGS);
        if (storagePermissionGranted == PackageManager.PERMISSION_GRANTED) {
            // Do logic
        } else {
            PermissionUtils.checkPermission(this, Manifest.permission.WRITE_APN_SETTINGS, 1);
        }
    }

And don't forget add on manifest the permission:

<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />

Upvotes: 0

Related Questions