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

Dobin picture Dobin · Aug 28, 2015 · Viewed 33.4k times · Source

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" />

Answer

japanjot singh picture japanjot singh · Feb 9, 2016

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