Android going to sleep (standby)

不想你离开。 提交于 2019-12-24 14:10:34

问题


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.


回答1:


private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;

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

Try this



来源:https://stackoverflow.com/questions/34285351/android-going-to-sleep-standby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!