Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing the display from a widget?

Tags:

android

I am trying to set the screen brightness from a widget. We know this is easily possible since tons of widgets do this already, but how.....

In a service I call from the widget I do this to set the brightness:

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

That works great except it does not "refresh" the screen to apply the new settings. Turning the screen off and on does refresh the display settings, so we know that the code works.

I also read on several sites that something like this will refresh the screen, but we cannot use this since we are in a widget. The widget activity and the service cannot use getWindow.

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

How else are all these widgets like Beautiful Widgets, Power control, Extended controls, etc doing this?

Update: Another poster recommended kicking off an empty activity and executing the WindowManager refresh. That works but it brings up an ugly black screen for a second. Since the other widgets don't do this, there has to be a way to keep the ugly blank black screen from showing.

like image 531
Cameron McBride Avatar asked Jan 10 '11 02:01

Cameron McBride


1 Answers

For the blackscreen, set a theme on the activity, containing this:

<style name="Theme.Transparent" parent="android:Theme"> 
<item name="android:windowIsTranslucent">true</item> 
<item name="android:windowBackground">@color/transparent</item> 
<item name="android:windowContentOverlay">@null</item> 
<item name="android:windowNoTitle">true</item> 
<item name="android:windowIsFloating">true</item> 
<item name="android:backgroundDimEnabled">false</item> 
</style>

Thanks Sebastian for the answer, I've copied it in here.

like image 92
Cameron McBride Avatar answered Sep 18 '22 12:09

Cameron McBride