0

I would like to write an activity that after clicking on a button turns off the screen and then turns it back on after 2 secs. I tried using the following code in order to power off the screen:

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0/(float)255;
getWindow().setAttributes(lp);

But it would only take effect when then onClick function returns. I tried running it into a handler but with no success. I need to find a way to force the setting to get applied before the function returns so that I can call the power on function 2 secs later on the same onClick call.

I also found it very hard to wakeup the device afterwards. While this code works if I power off the screen using the physical button it doesn't seem to work when the phone is powered off using the technique described previously.

PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE); 
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP | 
PowerManager.ON_AFTER_RELEASE ,"Dev Tag");
try
{
    wl.acquire();
    wl.release();
}
catch (Exception e)
{
    Toast.makeText(this, e.getMessage(),20).show();
}

Thanks you in advance for your help!

1 Answers1

0

I tried using the following code in order to power off the screen

That does not power off the screen. That sets the backlight to 0 brightness. You cannot power off the screen programmatically, last I checked.

But it would only take effect when then onClick function returns.

Correct. That is the way Android works. The UI is single-threaded. Your request to change the screen brightness is put on a queue, and that message will not be processed until you return control to Android by returning from onClick().

I need to find a way to force the setting to get applied before the function returns so that I can call the power on function 2 secs later on the same onClick call.

This is not possible. It is also not necessary. There are plenty of ways to get control after two seconds has elapsed. Probably the most efficient is to call postDelayed() on one of your widgets.

I also found it very hard to wakeup the device afterwards

The device was not asleep in the first place.

While this code works if I power off the screen using the physical button it doesn't seem to work when the phone is powered off using the technique described previously.

A WakeLock is something you use in a background service to keep the device awake for a few moments while you complete some work. It is also something used internally to handle thinks like android:keepScreenOn (used for ebook readers, video players, and other activities where the user might not tap the screen for a while). It has nothing to do with screenBrightness.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Very interesting comments thanks! I think however that setting the brigthness to 0 does turn off the screen: http://stackoverflow.com/questions/6756768/turn-off-screen-on-android – user1060919 Nov 23 '11 at 02:06
  • The only question that remain is how can I wake up the screen after the brightness is set to 0... – user1060919 Nov 23 '11 at 02:39