Programming powering off and powering on in one single OnClick function on android

Posted by user1060919 on Stack Overflow See other posts from Stack Overflow or by user1060919
Published on 2011-11-23T01:15:18Z Indexed on 2011/11/23 1:52 UTC
Read the original article Hit count: 161

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!

© Stack Overflow or respective owner

Related posts about android

Related posts about android-wake-lock