Changing screen brightness programmatically (as with the power widget)

I was searching how to change the brightness of the screen programmatically and I found this it is very good solution and it works nice, but it works only while my app is active. After my application is shutdown then the brightness is returned back the the same value as before I start my app.

I want to be able to change the brightness just like when I press on the brightness button from my power widget. In the power widget that comes from android there are 3 states. One very bright one very dark and one in between. Is it possible to change the brightness just like when someone press on this widget ?

enter image description here

Edit1: I created this and I added permision to my manifest but when the app is started I do not see any changes to the brightness, I tried with 10 with 100 and now with 200 but no changes any suggestions ?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
}

Solution 1:

This is the complete code I found to be working:

Settings.System.putInt(this.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS, 20);

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.2f;// 100 / 100.0f;
getWindow().setAttributes(lp);

startActivity(new Intent(this,RefreshScreen.class));

The code from my question does not work because the screen doesn't get refreshed. So one hack on refreshing the screen is starting dummy activity and than in on create of that dummy activity to call finish() so the changes of the brightness take effect.

Solution 2:

Use Tor-Morten's solution in that link, but also set the system brightness setting like so:

android.provider.Settings.System.putInt(getContext().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

Where bright is an integer ranging from 1 to 255.

Solution 3:

I have solved this problem today.

As the first you have to put a permission into your AndroidManifest.xml file:

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

Where is the exact place to put it in the file?

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

This permission says, that you are allowed to change settings that affect other applications too.

Now you can set brightness automatic mode on and off

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);  //this will set the automatic mode on
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)

Is the automatic mode turned on or off right now? You can get the information

int mode = -1;
try {
    mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); //this will return integer (0 or 1)
} catch (Exception e) {}

So, if you want to change brightness manually, you are supposed to set the manual mode first and after that you can change the brightness.

note: SCREEN_BRIGHTNESS_MODE_AUTOMATIC is 1

note: SCREEN_BRIGHTNESS_MODE_MANUAL is 0

You should use this

if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
    //Automatic mode
} else {
    //Manual mode
}

instead of this

if (mode == 1) {
    //Automatic mode
} else {
    //Manual mode
}

Now you can change the brightness manually

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);  //brightness is an integer variable (0-255), but dont use 0

and read brightness

try {
    int brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  //returns integer value 0-255
} catch (Exception e) {}

Now everything is set properly, but... you can't see the change yet. You need one more thing to see the change! Refresh the screen... so do this:

    try {
        int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  //this will get the information you have just set...

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = (float) br / 255; //...and put it here
        getWindow().setAttributes(lp);
    } catch (Exception e) {}

Don't forget the permission...

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

Solution 4:

Passing the context of the activity while setting up the parameters would also get the job done without any need to start another activity. The following worked for me-

WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.screenBrightness =0.00001f;// i needed to dim the display
this.getWindow().setAttributes(lp);

i had this piece of code inside onSensorChanged() method which dimmed the display whenever it was triggered.