Changing screen brightness efficiently - android

I need to change the screen brightness programmatically. I read multiple solutions like this Can't apply system screen brightness programmatically in Android.
My problem is that those solutions implies changing the activity to be effective (having something like a dummy activity finishing immediately) and I would like to avoid the overhead of an activity switch.
Is there any other solution... maybe using native code so that the screen brightness will change immediately ?

The following affects immediately the single activity, no need to restart it. The activity also remembers the screenBrightness attribute over pause/resume.
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1; // 0f - no backlight ... 1f - full backlight
getWindow().setAttributes(lp);
But it has no effect if you have automatic backlight level enabled in the system settings. This solution should help to turn off automatic backlight.

Related

Prevent screen from locking but allow dimming

I'm trying to get my Android app to keep the screen unlocked as long as the app is running. I easily solved this problem with setting this flag - WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON - in each activity.
But now I'd also like to allow the screen to dim and stay dimmed until a touch event is registered. I tried to mess with window object's screenBrightness and dimAmount but none of these worked (no changes in the brightness were made). Any ideas if I can implement this feature?
As a last resort I'd think a black dialog with alpha .5 covering the whole screen would work, but I don't know if this solution will have the same effect as an actual dim, battery-saving wise

Refreshing screen brightness instantaneously

I'm trying to set System Screen Brightness instantaneously. I'm using a service to do that. Here it the code that I'm using.
Settings.System.putInt(cResolver,
Settings.System.SCREEN_BRIGHTNESS, 255);
This code works fine. But it is not instantaneous. The screen brightness value changes but the screen still looks dull. I can confirm this by reading back the screen brightness value and it is 255. If I lock the screen and come back, the screen is fully bright. How can I achieve this that the moment I set the system brightness to the maximum, I see fully bright screen?
BTW, I want to do this using a service and not an activity!
I would take a look at this link, specifically the first answer. It's a bit hackish though.
Changing screen brightness programmatically (as with the power widget)
Essentially you need to force the screen to refresh through starting a dummy activity and then finish() said dummy activity. I would have commented but I don't have enough points.

Set brightness below the system limit using alpha and save value

I've been trying to set the brightness under the limit of Android settings. There are two similar apps in Google Play doing this:
Shades
Screen Filter
The second one, even allows to use the phone with a black screen, just what I need in my app.
There are a lot of other questions about brightness but not to set it under this system limit, and I've found one question asking almost the same but not helping me:
Brightness Screen Filter
So after a lot of search I've got following code:
int brightness=0; //0 to 255, so I set it to a low level (not enough for me)
ContentResolver cResolver = getContentResolver();
//set the system brightness value, and if active, disable auto mode.
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
System.putInt(cResolver, System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//previous lines don't set the brightness to the current screen so we set it like this:
LayoutParams layoutpars = getWindow().getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
//Now we have brightness to the minimum allowed by Android but
//to achieve what these apps do, I have to reduce alpha of the window to the min
//then the screen is black like I want, and totally usable!
layoutpars.alpha=0.05f;
window.setAttributes(layoutpars);
Here the alpha is the key, this is doing what I want, but only for current window/screen, and I need to save to the system this alpha as default, like I'm doing with the brightness and the Manual mode for applying everywhere.
Do you know how I should do this? I'm unable to find a value "System.SCREEN_ALPHA" to set it with System.putInt or another way to do this.
In the other question I linked before, pheelicks replied with a suggestion of using a transparent non-touchable over screen, but this is not working for me, and the result does not give the feeling of a turned off screen like previous apps.
Thanks.
I decided to implemented this feature just inside my app with alpha. So I couldn't solve the initial question at all....
Anyways, in the end seems that the solution to this question is the one Pheelicks replied here: https://stackoverflow.com/a/4287597/1667990
-This is launching an activity to be always on top,
-with alpha to 0.0f so it be transparent,
-that redirects the touch to the activity behind:
//Let touches go through to apps/activities underneath.
Window window = activity.getWindow();
window.addFlags(FLAG_NOT_TOUCHABLE);
-And most important thing not explained in the previous link, is to set the dim behind our window to 1 (like the dim in a messagebox, but setting to the max to be black behind :D )
window.setDimAmount ((float)1.0) ;
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
I haven't tried that and decided not to do it in my app as it could bring unexpected behavior in my circumstances, but I think it should definitely work, if anyone try please add your feedback!

Android: rotating to landscape

I have an Android app with an activity that processes the orientation change (android:configChanges="keyboardHidden|orientation" in the manifest). It also has an onConfigurationChanged handler that calls the superclass.
After the device is rotated, the activity remains in portrait mode. And the whole screen is in bizarre state where the X axis goes physically bottom to top, the Y axis goes left to right, the status bar runs along the left screen edge, and all text goes physically up. It's as if the system is forced to not acknowledge the orientation change.
What I would really like to do, I'd like to avoid activity restart, but process the orientation change with few carefully placed layout property flips. However, in the abovementioned state of the GUI I cannot do that. Reloading the main layout file makes no change.
Question - how do I tell the system to switch physical orientation, so that XY axes go the way they should?
EDIT: and here's a curious data point: on all onConfigChanged calls, the newConfig.orientation is 1.
You really shouldn't use android:configChanges. This technique is considered as a last resort. Please read Handling Runtime Changes for more details.
Brief explanation: if you used android:configChanges its your responsibility to handle configuration change event and reload the UI resources for new configuration. When onConfigurationChanged() is called, getResources() already points to correct resources from new configuration. You only need to manually re-set all layouts, strings, dimensions, drawables, etc. with new (potentially changed) values.
You also didn't mention reasons why you decided to go with configChanges, instead of normal Activity workflow. This might have been useful to give you some alternatives.
The attribute android:screenOrientation="portrait" in the manifest was to blame. With this attribute present, even letting the system restart the activity won't switch the screen to true landscape mode.

Changing screen brightness on a HTC Sense device

The old approach
Window window = getWindow();
LayoutParams layoutParams = window.getAttributes();
layoutParams.screenBrightness = newFloatValue;
window.setAttributes(layoutParams);
doesn't work on HTC Desire with Automatic brightness checked in Settings > Display > Brightness.
Is there a HTC-specific workaround for that?
Of course you don't see any changes since the settings are set to Automatic Brightness. That means, any manual changes are disregarded. Or am I missing something?
I've just tested it on a HTC Desire HD (2.3.5) and the screen brightness DOES change for about a second, but then it's immediately re-adjusted/overwritten by the automatic brightness. However, if you uncheck Auto Brightness option in the settings, you can see that the value has changed correctly.
So, imo everything is working as expected. If you want your changes to take effect you might have to disable Auto Brightness first:
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

Categories

Resources