Changing the screen brightness was easy in pre-cupcake times. Now the android.os.IHardware is not available anymore.
Given here are the steps to set the brightness (step 1) and make it effective immediately (step 2) plus a dirty workaround which is needed when you are exiting your application after applying the brightness change (step 3).
I hope to get rid of step 3 somehow and replace it with a cleaner solution!
// 1. change the system setting:
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
255); // 0-255
// 2. set the brightness for the current activity
lp = getWindow().getAttributes();
lp.screenBrightness = 1.0f; // 0.0 - 1.0
getWindow().setAttributes(lp);
// 3. wait for some time before finishing (this is only needed if
// you work without a visible activity or if you want to exit
// directly after applying the changes. because if you exit
// immediately the brightness will not be applied immediately but
// delayed)
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
this.cancel();
finish();
}
}, 1000, 5000);
1 Comment
What Object type is lp? I tried storing lp in WindowManager.LayoutParams and LayoutParams and both approaches cause a compilation error at line 10