title: How to turn screen off or send device to sleep
body: <p>I want to send device to sleep or turn the screen off. I have investigated and found this topic: <a href="https://stackoverflow.com/questions/6756768/turn-off-screen-on-android/6757206#6757206">Turn off screen on Android</a></p>
  
  <p>Basically, the are three ways to do it, but I have found problems for the three:</p>
  
  <p>a) Choice 1:</p>
  
  <pre><code>PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
  manager.goToSleep(int amountOfTime);
  </code></pre>
  
  <p><strong>Problem:</strong> It causes FC. I have read I need <code>DEVICE_POWER</code> permissions, but it can't be granted for normal apps.</p>
  
  <p>b) Choice 2:</p>
  
  <pre><code>PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
  PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
  wl.acquire();
  wl.release();
  </code></pre>
  
  <p><strong>Problem:</strong> This does not work for me. I don't know why. It does not give me a FC, but it is innocuous.</p>
  
  <p>c) Choice 3:</p>
  
  <pre><code>WindowManager.LayoutParams params = getWindow().getAttributes();
  params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
  params.screenBrightness = 0;
  getWindow().setAttributes(params);
  </code></pre>
  
  <p><strong>Problem:</strong> I can get it to work but when try to turn on device it does strange things, like don't like to return, or if my apps is in front, automatically goes to sleep just after pressing on button. This looks more like a tip or workaround than normal solution.</p>
  
  <p>Can anyone tell me any good method to send device to sleep or turn screen off that can run without problems? It sound rare to me that a simple functionality like this has not a good way to use it (or at lest well documented)</p>