Android: How to turn screen on and off programmatically?
<p>Before marking this post as a "duplicate", I am writing this post because no other post holds the solution to the problem.</p>

<p>I am trying to turn off the device, then after a few minutes or sensor change, turn it back on.</p>

<p><strong>Turn Off Display Tests</strong></p>

<p>I am able to turn off the screen using:</p>

<pre><code>params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
</code></pre>

<p>I have been unable to turn off the screen using the wl.release() method.</p>

<p><strong>Turn On Display Test</strong></p>

<p>My first guess, as follows, does not work. Nothing happens, screen remains off.</p>

<pre><code>params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = -1f;
getWindow().setAttributes(params);
</code></pre>

<p>I also then tried to use wakelocks, with no success.</p>

<pre><code>PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "tag");
    wl.acquire();
</code></pre>

<p>Finally I have tried the following, with no result.</p>

<pre><code>getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
</code></pre>

<p>All in all, I don't get any kind of error in the console for any of these methods. My test text "Screen should be on", is on the the screen when I turn on the device using the power button. This shows that the code should have ran. Please only answer if you have tested the code, it seems like many of the functions such as params.screenBrightness = -1, do not work as they should according to the sdk.</p>

<p>Thanks!</p>
