0

I'm trying to change my system/phone brightness through my application. For this I need to ask for permission ("dangerous permission"). I'm not quiet sure if I've done the correct way but this doesn't seem work as it always returns permission denied.

public void switches() {
            if (systemBrightness) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.WRITE_SETTINGS},
                        1);
                Toast.makeText(this, "Changed to system brightness: changing the “system brightness” is permanent", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Changed to screen brightness: ", Toast.LENGTH_SHORT).show();
            }
        }


public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {

                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

//Mani fest

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />




  public void changeScreenBrightness(float brightness) {
        mBrightness = brightness;
        if (systemBrightness) {
            if (!Settings.System.canWrite(this)) {
                Intent i = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivity(i);
            } else {
                Settings.System.putInt(mContentResolver, Settings.System.SCREEN_BRIGHTNESS, (int) (mBrightness * 255)* preset);
            }
        } else {
            WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
            mLayoutParams.screenBrightness = mBrightness * preset;
            mWindow.setAttributes(mLayoutParams);
        }
    }
Amar
  • 509
  • 2
  • 15
  • 36

1 Answers1

1

Asking for dangerous permission is handled differently than vulnerable permissions. You need to use Settings.System.canWrite(Context) method to check if you can do that. And if not send appropriate Intent to the system by calling startActivityForResult:

public boolean checkWriteSettingsPermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (!Settings.System.canWrite(getContext())) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
            Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, REQUEST_CODE);
        return false;
    } else {
        return true;
    }
}

Everything written in the documentation

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
  • I have edited my question with the intent permission. I'm now getting up a new intent/window that is titled "Aviability". But nowhere here will let me accept the permission to let the app change system screen brightness – Amar Jun 06 '17 at 21:23
  • Is automatic brightness set? Checkout this answer: https://stackoverflow.com/a/36001592/6507689 – R. Zagórski Jun 07 '17 at 05:58
  • Yes ok but this is an outdated answer. As in since the API level of my application is 23 the permission declaration in manifest wont work? – Amar Jun 07 '17 at 08:48
  • My answer shows how to get `WRITE_SETTINGS` permission on API > 23. Linked answer shows show to check if automatic/manual screen brightness mode is set and how to set value. – R. Zagórski Jun 07 '17 at 10:51
  • Thanks for the help! I just found what the problem was, a simple typo. Instead of asking for `ACTION_MANAGE_WRITE_SETTINGS` I'm asking for `Intent i = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)` – Amar Jun 07 '17 at 11:21