Search code examples
androidbrightness

cannot set brightness for application


I'm writing an app to change screen brightness with seekbar and I use this code on event onProgressChanged

Settings.System.putInt(getApplicationContext().getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,newBrightness);

but the application crashed after this code. I also added

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

Does anyone know why? please help me

Thanks in advance


Solution

  • You should refresh the screen too. It's more complicated that your code.

    Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 20);
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = 0.2f;
    getWindow().setAttributes(layoutParams);
    startActivity(new Intent(this,RefreshScreen.class));
    

    And also you may need to set automatic mode off :

    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);