Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 6.0 Permission Denial: requires permission android.permission.WRITE_SETTINGS

As you can see from my manifest below, I've added the permission,What am I missing?

<uses-permission-sdk-m android:name="android.permission.WRITE_SETTINGS" />
like image 618
Dobin Avatar asked Aug 28 '15 08:08

Dobin


3 Answers

In API 23 or higher user has to authorize manually for this permission, you can check by calling- 'Settings.System.canWrite' below is the implementation for this:-

           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.System.canWrite(context)) {
                    // Do stuff here
                }
                else {
                    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
like image 139
japanjot singh Avatar answered Oct 14 '22 05:10

japanjot singh


It turns out you need to use a different mechanism to be granted WRITE_SETTINGS in Android 6. requestPermissions doesn't work, but CommonsGuy has provided a workaround here: https://stackoverflow.com/a/32083622/238753

like image 44
Sam Avatar answered Oct 14 '22 04:10

Sam


   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.System.canWrite(getApplicationContext())) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 200);
        }
    }

this works like a charm.

like image 3
Mihir Patel Avatar answered Oct 14 '22 04:10

Mihir Patel