Java android change brightness Android 6.0

132 Views Asked by At

This is my code to change screen brightness :

    protected void changeBrightness(int progress, int lvl) {
        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE,
                android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        android.provider.Settings.System.putInt(this.getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS, progress);
        float brightness = (float) progress / 255;
        if (brightness > 0.0f && brightness < 1.0f) {
            Intent intent = new Intent(this, Bright.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("brightness value", brightness);
            intent.putExtra("brightness lvl", lvl);
            getApplication().startActivity(intent);
        }
    }

I've added this to AndroidManifest.xml :

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

And in logs (into play store) in console developers, I see this :

    java.lang.SecurityException: 
      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.call (ContentProviderNative.java:646)
      at android.provider.Settings$NameValueCache.putStringForUser (Settings.java:1322)
      at android.provider.Settings$System.putStringForUser (Settings.java:1652)
      at android.provider.Settings$System.putIntForUser (Settings.java:1757)
      at android.provider.Settings$System.putInt (Settings.java:1751)
      at com..rozklad.RozkladPdfActivity.changeBrightness (RozkladPdfActivity.java:456)

Line 456 is this :

    android.provider.Settings.System.putInt(getContentResolver(),
0

There are 0 best solutions below