1

I want my android device to go to sleep (screen off), has someone an idea how to do it?

  • In the past I was using the PowerManager function goToSleep(time), but apparently this function does not exist anymore, I don't understand why.

  • Other topics talk about those lines:

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My wakelook");
        wakeLock.acquire();
    

But it only allows the device to enter sleep mode, without telling him to go to sleep mode now.

  • I already tried the solution which consists of diminishing the luminosity of the screen, but it is not a real solution since the device doesn't really enter sleep mode. And I don't want to use the following code:

    android.provider.Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, timeoutInactivity);
    

    because I don't want to touch the screen display settings and it is more a workaround than a solution.

PS : It's for a system app, so I don't have any right/permission limitation.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Stochelo
  • 153
  • 11
  • There is a goToSleep(time) method in power manager class. Just check that.But for sleep you should disconnect your USB source. – Madhukar Hebbar Dec 15 '15 at 09:28
  • Thanks for your answer. Unfortunately, I'm working with API23 and goToSleep(long) has been removed. When i try "pm.goToSleep(10);", android studio tells me "cannot resolve method goToSleep(long)". – Stochelo Dec 15 '15 at 09:40
  • Check [this question](http://stackoverflow.com/q/28459287/1276636). I think the comments there will be helpful. – Sufian Dec 15 '15 at 10:05
  • Thanks, unfortunately I didn't find a solution working in the topic. – Stochelo Dec 15 '15 at 14:06

1 Answers1

0
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;

public void sleep(){
      mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
      mWakeLock.acquire();
}

Try this