changing screen brightness

Put your problem here if it does not fit any of the other categories.

changing screen brightness

Postby spirolateral » Tue Oct 28, 2008 8:56 pm

does anyone know how to change screen brightness in an app? here is what i've tried so far:

to set:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightness);
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4



to get:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. brightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
  3.  
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



so i set it to a certain level, then read it and it corresponds to what i set. so it's doing something, but it's not changing any brightness levels. the problem might have something to do with the "ContentResolver". i don't really know what a ContentResolver is or if i'm using the correct one. any ideas?
spirolateral
Developer
Developer
 
Posts: 25
Joined: Tue Oct 14, 2008 5:15 am

Top

Postby haden » Wed Oct 29, 2008 10:07 am

If this is done on the emulator then i guess you won't see any change..
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby kinscore » Wed Oct 29, 2008 3:27 pm

I've tried the same as spirolateral, on a G1, also with no visible results.
The ineffectiveness of this code isn't limited to the emulator.
kinscore
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 3:22 pm

Postby haden » Wed Oct 29, 2008 3:33 pm

Did you put the appropriate permissions in your manifest files? im not sure which one it uses..
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby kinscore » Wed Oct 29, 2008 3:35 pm

android.permission.WRITE_SETTINGS
I have this in my manifest. Before I did it would crash with something like a permission denied exception.
kinscore
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 3:22 pm

Postby kinscore » Wed Oct 29, 2008 4:22 pm

Actually it "does" work...just very slowly. Much too slowly to be useful in the way I wanted to use it (for a flashlight/morse code signaling program--for which I can set the background color to black, but that isn't as dark and actually increases battery consumption).
So I looked into the source code for the built-in settings application (git://git.source.android.com/platform/p ... ttings.git).
It doesn't use Settings.System until "OK" is pressed. It does the real-time brightness setting with non-public IHardwareService and ServiceManager.

So, if a program is to set the brightness "some time in the future", system-wide, it can go ahead and use Settings.System. If it is to set the brightness "right now", locally, it can't (as far as I can tell). It has to wait until Android gets a public hardware service or similar API.
kinscore
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 3:22 pm

Top

Postby haden » Wed Oct 29, 2008 4:36 pm

Well.. screen brightnes doesnt get your screen completely dark. Why don't you just draw a white and black background instead?
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby kinscore » Wed Oct 29, 2008 4:43 pm

That's what I'm doing for now but I would prefer to get the screen darker still, and reduce battery consumption, by turning off the backlight.
kinscore
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 3:22 pm

Postby haden » Wed Oct 29, 2008 4:52 pm

Well turning off the back light is not the same as reducing screen dimness.. did you try the android.os.PowerManager class? that might have something you are looking for..
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby kinscore » Wed Oct 29, 2008 5:35 pm

I would have expected backlight value of 0 to mean off and 1 to mean the dimmest setting while on.
PowerManager has wake locks, some of which allow the screen to dim or turn off, but none which actually turn off the backlight or screen if it's currently on. Besides that it only has functions to force the device fully awake and fully asleep.
kinscore
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 3:22 pm

Postby spirolateral » Fri Oct 31, 2008 1:18 am

kinscore wrote:Actually it "does" work...just very slowly. Much too slowly to be useful in the way I wanted to use it (for a flashlight/morse code signaling program--for which I can set the background color to black, but that isn't as dark and actually increases battery consumption).
So I looked into the source code for the built-in settings application (git://git.source.android.com/platform/p ... ttings.git).
It doesn't use Settings.System until "OK" is pressed. It does the real-time brightness setting with non-public IHardwareService and ServiceManager.

So, if a program is to set the brightness "some time in the future", system-wide, it can go ahead and use Settings.System. If it is to set the brightness "right now", locally, it can't (as far as I can tell). It has to wait until Android gets a public hardware service or similar API.


so, what is this "OK" you're talking about? if i set the Settings.System, can i force the press of this "OK"?
spirolateral
Developer
Developer
 
Posts: 25
Joined: Tue Oct 14, 2008 5:15 am

Postby kinscore » Fri Oct 31, 2008 1:34 am

Interesting idea but actually it's a bit more complicated than that and the behavior is basically opposite what you suggest. The "OK" button is in the system settings user interface on the phone (and unavailable on the emulator). When "OK" is pressed it applies the action available in the public APIs. What's different is that when you're moving the slider in the built-in system settings brightness program, it uses IHardwareService, which isn't publicly available, to immediately change the brightness. I don't think the brightness setting interface is available to other programs, either.

So immediate changes to brightness are unavailable publicly.

Delayed changes in brightness are available with the code in the first post.

I don't know whether it can be seen in the emulator at all. With hardware running Android natively (like the T-Mobile G1), you might be able to see the change by locking the screen and then unlocking it again or you might have to wait a few minutes...

This explanation gives me an idea that might be a workaround in some cases but is kind of tricky and probably too slow for some applications. After Settings.System.putInt(), use PowerManager.goToSleep() followed by PowerManager.userActivity(). userActivity() might have to be set in a timer or some such before goToSleep() in order to avoid being canceled by the phone going to sleep...I really don't recommend this and won't be using it myself--but if you absolutely must have brightness settings applied now, give it a try...at your own risk.
kinscore
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 3:22 pm

Postby spirolateral » Fri Oct 31, 2008 1:38 am

i'll might try that out. i downloaded a "flashlight" app the other day and it increased my brightness. there is a way to do this.
spirolateral
Developer
Developer
 
Posts: 25
Joined: Tue Oct 14, 2008 5:15 am

Postby kinscore » Fri Oct 31, 2008 1:55 am

spirolateral wrote:i'll might try that out. i downloaded a "flashlight" app the other day and it increased my brightness. there is a way to do this.


Ah, yes, increasing brightness is easy. Just use a wakelock with the screen bright flag. Dimming or turning off the backlight, on the other hand, is either delayed, impossible or requiring weird tricks as I've said.
kinscore
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 29, 2008 3:22 pm

Postby spirolateral » Fri Oct 31, 2008 2:11 am

kinscore wrote:
spirolateral wrote:i'll might try that out. i downloaded a "flashlight" app the other day and it increased my brightness. there is a way to do this.


Ah, yes, increasing brightness is easy. Just use a wakelock with the screen bright flag. Dimming or turning off the backlight, on the other hand, is either delayed, impossible or requiring weird tricks as I've said.


when i tried with that screen bright flag it just keeps it at the system set brightness level until the wake lock is released. it didn't increase the brightness.
spirolateral
Developer
Developer
 
Posts: 25
Joined: Tue Oct 14, 2008 5:15 am

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests