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 ?
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);
}
I have solved this problem today.
As the first you have to put a permission into your AndroidManifest.xml file:
Where is the exact place to put it in the file?
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
Is the automatic mode turned on or off right now? You can get the information
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
instead of this
Now you can change the brightness manually
and read brightness
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:
Don't forget the permission...
Use Tor-Morten's solution in that link, but also set the system brightness setting like so:
Where
bright
is an integer ranging from 1 to 255.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-
i had this piece of code inside onSensorChanged() method which dimmed the display whenever it was triggered.
This is the complete code I found to be working:
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.COMPLEX EXAMPLE: