21

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" />
Dobin
  • 239
  • 1
  • 2
  • 3
  • https://developer.android.com/preview/features/runtime-permissions.html – Budius Aug 28 '15 at 08:24
  • I had already read it .the requestPermissions() method doesn't work,why? – Dobin Aug 28 '15 at 08:32
  • maybe you should check that then: http://stackoverflow.com/help/on-topic and that http://stackoverflow.com/help/dont-ask – Budius Aug 28 '15 at 08:34
  • 1
    possible duplicate of [Android M 6.0 RingtoneManager - Manifest.permission.WRITE\_SETTINGS Error](http://stackoverflow.com/questions/32083410/android-m-6-0-ringtonemanager-manifest-permission-write-settings-error) – Sam Aug 28 '15 at 09:17

7 Answers7

59

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);
                }
            }
David Liu
  • 9,426
  • 5
  • 40
  • 63
japanjot singh
  • 1,052
  • 7
  • 8
  • 3
    How can I handle the result of the activity in else condition? How could I know if my app granted the permission? – Mohammed AlBanna May 08 '16 at 00:19
  • 3
    @Mohammad you could check again in ````onResume()```` – xorgate Jun 09 '16 at 13:16
  • 1
    If brightness is not in manual mode, this doesn't work. to set brightness to be manual, do: `Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);` http://stackoverflow.com/a/36001592/175225 – Ying Mar 01 '17 at 16:59
  • i've tried this for changing ringtone dynamically but didn't worked. check with my new answer and see if works for you. – Mihir Patel Apr 03 '17 at 20:15
4

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

Community
  • 1
  • 1
Sam
  • 40,644
  • 36
  • 176
  • 219
3
   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.

Mihir Patel
  • 456
  • 7
  • 20
0

This is a special case: permision level:signature ,

If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen.

The app requests the user's approval by sending an intent with action ACTION_MANAGE_WRITE_SETTINGS.

The app can check whether it has this authorization by calling Settings.System.canWrite().

Feng.G
  • 1
0
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            boolean settingsCanWrite = Settings.System.canWrite(this);

            if(!settingsCanWrite) {
                Toast.makeText(this, "Require Permission to Handle Screen Brightness", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                startActivity(intent);
            }
        }
0
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);
            }
        }
ChristianYami
  • 586
  • 1
  • 9
  • 17
-1

This is dependent on the API level and the Android build version