Update Display Brightness on Android after changing it programmatically
Asked Answered
D

3

9

I'm trying to update the display brightness from a widget but i have some problems.

To change brightness level, i use:

Settings.System.putInt(context.getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);

This modifies the display setting (in fact in Display->Brightness the level is correct) but the effective brightness of display is not changed. If i lock the screen and unlock, the brightness finally changes to the value i set.

I assume this is a Settings Update issue, so how can the display settings be immediatly updated after settings change?

I read that WindowManager.LayoutParams lp = getWindow().getAttributes(); should be used but I am working in a App Widget so getWindow() cannot be called.

Donaldson answered 6/12, 2010 at 12:12 Comment(0)
M
6

I had a similar issue and just created an Activity with no UI to do the brightness change, used an intent to run it from the App Widget.

Matamoros answered 6/12, 2010 at 12:26 Comment(8)
Good idea! You created a new class Activity or bound it inside the widget?Donaldson
Since you resolved the issue, can you write down some code to explain clearly? Thanks GeekYouUp!Donaldson
I tried to create an activity, but the change of brightness is applied to the activity only...Donaldson
Hi. Must be some time difference between us, but I'm back now. My brightness setting project is open source the code is at autobright.googlecode.com I always need a 500ms wait before finishing the Activity if I want the brightness to be displayed across the system.Matamoros
Thanks! I'll try to figure this out this way, I'll let you know!Donaldson
@Matamoros How can i create dummy activity??Lyophilic
@Donaldson How to make dummy activity as GeekYouUp said.Lyophilic
@Matamoros How to change brightness in Service while I'm using another application?Bradshaw
T
1

First, the value to modify in LayoutParams is screenBrightness. You'll then have to do a window.setAttributes to apply it. As GeekYouUp said, you can make a dummy activity to get your Window object.

Towrope answered 6/12, 2010 at 12:36 Comment(2)
How can i create dummy activity??Lyophilic
as he said, you create an activity, just associated to no UITowrope
R
0

Can you use this code in your RemoteView,

Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);

// This makes the new screen brightness effective
WindowManager.LayoutParams layoutParams = ((Activity)context).getWindow().getAttributes();
float b = brightness/255.0f;
if(b == 0.0)    
    b = 0.01f;
layoutParams.screenBrightness = b;
((Activity)context).getWindow().setAttributes(layoutParams);

This code fine works when you are setting phone screen brightness from inside a User-defined class which is not extending an Activity but you only need the context.

Rudin answered 13/12, 2014 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.