Setting the brightness in Android - Nothing worked so far
-
09-12-2019 - |
Question
Hi I tried all the solutions on the web and in SO but nothing works. I have an overlay (service) that pops up full screen when you click the trigger. On the overlay I have put some of shortcuts, volume controls and brightness control. The point is that no matter what I do the brightness does not immediately change the screen (I need to switch the screen off and on).
This is what I call when the brightness slider is updated:
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
int value = (int)(arg1*255.0/100);
int V=-1;
try {
V=android.provider.Settings.System.getInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("V="+V+"brightness="+value);
//android.provider.Settings.System.putInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
android.provider.Settings.System.putInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, value);
android.provider.Settings.System.putInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
// Intent intent= new Intent(getContext(), Bright.class);
// Bundle b=new Bundle();
// b.putFloat("lvl", (float)(arg1*0.01));
// intent.putExtras(b);
// //intent.putExtra("lvl", (float)(arg1*0.01));
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// getContext().startActivity(intent);
}
});
that works if you turn the screen off and on but not otherwise. Since the class is a service (no getWindow() available), I also tried the trick of spawning a fake Activity which calls the code (also found on the web)
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness=0.9f;
getWindow().setAttributes(lp);
Unfortunately none of the above solutions seem to work at all in my case. The WindowManager seems to do nothing. Probably it's the way my app is implemented (system overlay rather than a "classic" window)
Anyways, all of a sudden I noticed that the slider started to work when I left the Settings>Brightness window open in the background. That way the slider would have direct control on the system brightness by just setting two lines
android.provider.Settings.System.putInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, value); android.provider.Settings.System.putInt(getContext().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
So this means that when there is "some process" happening in the background the regulation of the brightness is very easy (as it SHOULD!). Can anybody help me try to understand HOW I can get the same effect? I would be alright to simply launch the brightness window with a command line (if I knew how) but I would obviously like a more subtle solution.
Please let me know what you think.
cheers
Emanuele
Solution
Use this, it should work:
final WindowManager.LayoutParams window_params=getWindow().getAttributes();
window_params.screenBrightness=1f;
getWindow().setAttributes(window_params);
screenBrightness is betwen 0.0f and 1.0f.
//sorry, I noticed you already tried this code. I have not been working with overlay apps so can't help you there.