I'm attempting to change the screen brightness from withing a service, like so:
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);
Problem is that is doesn't work. Well, actually it succeeds in changing the brightness setting, but the screen brightness doesn't actually change till I go into the phones settings, look at the new value and hit Ok.
Is there something I have to do after setting the value to get the brightness to change?
You might have to do it through the
Window
object.For example,
A less intrusive way (using significantly less code) is:
I've had the same problem of changing screen brightness from within a service, and a couple days ago i have successfully solved it(and updated my app Phone Schedule with brightness feature ;) ). Ok, so this is the code you put into your service:
Please Note that in the above code snippet I'm using two variables for brightness. One is
brightness
, which is a float number between 0.0 and 1.0, the other one isbrightnessInt
, which is an integer between 0 and 255. The reason for this is thatSettings.System
requires an integer to store system wide brightness value, while thelp.screenBrightness
which you will see in the next code snippet requires a float. Don't ask me why not use the same value, this is just the way it is in Android SDK, so we're just going to have to live with it.This is the code for DummyBrightnessActivity:
This is how you add your activity to the AndroidManifest.xml, probably the most important part:
A little explanation about what's what.
android:taskAffinity
must be different, than your package name! It makes DummyBrightnessActivity be started not in your main stack of activities, but in a separate, which means that when DummyBrightnessActivity is closed, you won't see the next activity, whatever that may be. Until i included this line, closing DummyBrightnessActivity would bring up my main activity.android:excludeFromRecents="true"
makes this activity not available in the list of recently launched apps, which you definetely want.android:theme="@style/EmptyActivity"
defines the way DummyBrightnessActivity looks like to the user, and this is where you make it invisible. This is how you define this style in the styles.xml file:This way your DummyBrightnessActivity will be invisible to the user. I'm not shure if all of those style parameters are really necessary, but it works for me this way.
I hope that explains it, but if you have any questions, just let me know.
I just checked in JB. The following code is enough to set brightness from a service
first line overrides the auto brightness.